root/src/xdisp.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. it_char_has_category
  2. char_can_wrap_before
  3. char_can_wrap_after
  4. fill_column_indicator_column
  5. redisplay_other_windows
  6. wset_redisplay
  7. fset_redisplay
  8. bset_redisplay
  9. bset_update_mode_line
  10. wset_update_mode_line
  11. ATTRIBUTE_FORMAT_PRINTF
  12. ATTRIBUTE_FORMAT_PRINTF
  13. window_text_bottom_y
  14. window_box_width
  15. window_box_height
  16. window_box_left_offset
  17. window_box_right_offset
  18. window_box_left
  19. window_box_right
  20. window_box
  21. window_box_edges
  22. line_bottom_y
  23. DEFUN
  24. default_line_pixel_height
  25. string_from_display_spec
  26. window_hscroll_limited
  27. reset_box_start_end_flags
  28. pos_visible_p
  29. check_char_and_length
  30. string_pos_nchars_ahead
  31. string_pos
  32. c_string_pos
  33. number_of_chars
  34. compute_string_pos
  35. estimate_mode_line_height
  36. pixel_to_glyph_coords
  37. x_y_to_hpos_vpos
  38. frame_to_window_pixel_xy
  39. get_glyph_string_clip_rects
  40. get_glyph_string_clip_rect
  41. get_phys_cursor_geometry
  42. remember_mouse_glyph
  43. adjust_window_ends
  44. hscrolling_current_line_p
  45. safe_eval_handler
  46. safe__call
  47. safe_call
  48. safe_call1
  49. safe__call1
  50. safe_eval
  51. safe__eval
  52. safe_call2
  53. CHECK_IT
  54. CHECK_WINDOW_END
  55. init_iterator
  56. get_narrowed_width
  57. get_narrowed_len
  58. get_medium_narrowing_begv
  59. get_medium_narrowing_zv
  60. get_nearby_bol_pos
  61. get_small_narrowing_begv
  62. get_large_narrowing_begv
  63. get_large_narrowing_zv
  64. unwind_narrowed_begv
  65. start_display
  66. in_ellipses_for_invisible_text_p
  67. init_from_display_pos
  68. init_to_row_start
  69. init_to_row_end
  70. handle_stop
  71. compute_stop_pos
  72. compute_display_string_pos
  73. compute_display_string_end
  74. handle_fontified_prop
  75. face_at_pos
  76. handle_face_prop
  77. underlying_face_id
  78. face_before_or_after_it_pos
  79. handle_invisible_prop
  80. setup_for_ellipsis
  81. find_display_property
  82. get_display_property
  83. display_min_width
  84. handle_display_prop
  85. handle_display_spec
  86. display_prop_end
  87. handle_single_display_spec
  88. display_prop_intangible_p
  89. single_display_spec_string_p
  90. display_prop_string_p
  91. string_buffer_position_lim
  92. string_buffer_position
  93. handle_composition_prop
  94. handle_overlay_change
  95. next_overlay_string
  96. compare_overlay_entries
  97. load_overlay_strings
  98. get_overlay_strings_1
  99. get_overlay_strings
  100. push_it
  101. iterate_out_of_display_property
  102. restore_face_box_flags
  103. pop_it
  104. back_to_previous_line_start
  105. strings_with_newlines
  106. forward_to_next_line_start
  107. back_to_previous_visible_line_start
  108. reseat_at_previous_visible_line_start
  109. reseat_at_next_visible_line_start
  110. reseat
  111. reseat_1
  112. reseat_to_string
  113. lookup_glyphless_char_display
  114. merge_escape_glyph_face
  115. merge_glyphless_glyph_face
  116. forget_escape_and_glyphless_faces
  117. get_next_display_element
  118. set_iterator_to_next
  119. next_element_from_display_vector
  120. get_visually_first_element
  121. next_element_from_string
  122. next_element_from_c_string
  123. next_element_from_ellipsis
  124. next_element_from_image
  125. next_element_from_xwidget
  126. next_element_from_stretch
  127. compute_stop_pos_backwards
  128. handle_stop_backwards
  129. next_element_from_buffer
  130. next_element_from_composition
  131. move_it_in_display_line_to
  132. move_it_in_display_line
  133. move_it_to
  134. move_it_vertically_backward
  135. move_it_vertically
  136. move_it_past_eol
  137. move_it_by_lines
  138. partial_line_height
  139. fast_move_it_horizontally
  140. in_display_vector_p
  141. window_text_pixel_size
  142. DEFUN
  143. format_nargs
  144. add_to_log
  145. vadd_to_log
  146. message_log_maybe_newline
  147. message_dolog
  148. message_log_check_duplicate
  149. message3
  150. message_to_stderr
  151. message3_nolog
  152. message1
  153. message1_nolog
  154. message_with_string
  155. ATTRIBUTE_FORMAT_PRINTF
  156. message
  157. update_echo_area
  158. ensure_echo_area_buffers
  159. with_echo_area_buffer
  160. with_echo_area_buffer_unwind_data
  161. unwind_with_echo_area_buffer
  162. setup_echo_area_for_printing
  163. display_echo_area
  164. display_echo_area_1
  165. resize_echo_area_exactly
  166. resize_mini_window_1
  167. resize_mini_window
  168. current_message
  169. current_message_1
  170. push_message
  171. restore_message
  172. pop_message_unwind
  173. check_message_stack
  174. clear_message_stack
  175. truncate_echo_area
  176. truncate_message_1
  177. set_message
  178. set_message_1
  179. clear_message
  180. clear_garbaged_frames
  181. echo_area_display
  182. window_buffer_changed
  183. mode_line_update_needed
  184. window_frozen_p
  185. format_mode_line_unwind_data
  186. unwind_format_mode_line
  187. store_mode_line_noprop_char
  188. store_mode_line_noprop
  189. gui_consider_frame_title
  190. needs_no_redisplay
  191. prepare_menu_bars
  192. update_menu_bar
  193. restore_selected_window
  194. restore_frame_selected_window
  195. update_tab_bar
  196. display_tab_bar
  197. build_desired_tab_bar_string
  198. display_tab_bar_line
  199. tab_bar_height
  200. redisplay_tab_bar
  201. tab_bar_item_info
  202. get_tab_bar_item
  203. handle_tab_bar_click
  204. note_tab_bar_highlight
  205. tty_get_tab_bar_item
  206. tty_handle_tab_bar_click
  207. update_tool_bar
  208. build_desired_tool_bar_string
  209. display_tool_bar_line
  210. tool_bar_height
  211. redisplay_tool_bar
  212. tool_bar_item_info
  213. get_tool_bar_item
  214. handle_tool_bar_click_with_device
  215. handle_tool_bar_click
  216. note_tool_bar_highlight
  217. hscroll_window_tree
  218. hscroll_windows
  219. debug_method_add
  220. text_outside_line_unchanged_p
  221. redisplay
  222. overlay_arrow_string_or_property
  223. overlay_arrow_in_current_buffer_p
  224. overlay_arrows_changed_p
  225. update_overlay_arrows
  226. overlay_arrow_at_row
  227. check_point_in_composition
  228. reconsider_clip_changes
  229. propagate_buffer_redisplay
  230. redisplay_internal
  231. unwind_redisplay_preserve_echo_area
  232. redisplay_preserve_echo_area
  233. unwind_redisplay
  234. unwind_display_working_on_window
  235. mark_window_display_accurate_1
  236. mark_window_display_accurate
  237. disp_char_vector
  238. block_buffer_flips
  239. unblock_buffer_flips
  240. buffer_flipping_blocked_p
  241. redisplay_windows
  242. redisplay_window_error
  243. redisplay_window_0
  244. redisplay_window_1
  245. update_redisplay_ticks
  246. set_cursor_from_row
  247. run_window_scroll_functions
  248. cursor_row_fully_visible_p
  249. try_scrolling
  250. compute_window_start_on_continuation_line
  251. try_cursor_movement
  252. set_vertical_scroll_bar
  253. set_horizontal_scroll_bar
  254. window_start_acceptable_p
  255. DEFUN
  256. redisplay_window
  257. try_window
  258. try_window_reusing_current_matrix
  259. find_last_row_displaying_text
  260. find_last_unchanged_at_beg_row
  261. find_first_unchanged_at_end_row
  262. sync_frame_with_window_matrix_rows
  263. row_containing_pos
  264. try_window_id
  265. dump_glyph_row
  266. dump_glyph
  267. dump_glyph_row
  268. DEFUN
  269. DEFUN
  270. DEFUN
  271. get_overlay_arrow_glyph_row
  272. insert_left_trunc_glyphs
  273. row_hash
  274. compute_line_metrics
  275. clear_position
  276. append_space_for_newline
  277. extend_face_to_end_of_line
  278. trailing_whitespace_p
  279. highlight_trailing_whitespace
  280. row_for_charpos_p
  281. cursor_row_p
  282. push_prefix_prop
  283. get_it_property
  284. get_line_prefix_it_property
  285. handle_line_prefix
  286. unproduce_glyphs
  287. find_row_edges
  288. display_count_lines_logically
  289. display_count_lines_visually
  290. maybe_produce_line_number
  291. should_produce_line_number
  292. row_text_area_empty
  293. display_line
  294. DEFUN
  295. DEFUN
  296. DEFUN
  297. display_menu_bar
  298. deep_copy_glyph_row
  299. display_tty_menu_item
  300. redisplay_mode_lines
  301. display_mode_lines
  302. display_mode_line
  303. move_elt_to_front
  304. safe_set_text_properties
  305. display_mode_element
  306. store_mode_line_string
  307. pint2str
  308. pint2hrstr
  309. decode_mode_spec_coding
  310. percent99
  311. decode_mode_spec
  312. count_lines
  313. display_count_lines
  314. display_string
  315. invisible_prop
  316. DEFUN
  317. calc_pixel_width_or_height
  318. get_font_ascent_descent
  319. dump_glyph_string
  320. init_glyph_string
  321. append_glyph_string_lists
  322. prepend_glyph_string_lists
  323. append_glyph_string
  324. get_char_face_and_encoding
  325. get_glyph_face_and_encoding
  326. get_char_glyph_code
  327. fill_composite_glyph_string
  328. fill_gstring_glyph_string
  329. fill_glyphless_glyph_string
  330. fill_glyph_string
  331. fill_image_glyph_string
  332. fill_xwidget_glyph_string
  333. fill_stretch_glyph_string
  334. get_per_char_metric
  335. normal_char_ascent_descent
  336. normal_char_height
  337. gui_get_glyph_overhangs
  338. left_overwritten
  339. left_overwriting
  340. right_overwritten
  341. right_overwriting
  342. set_glyph_string_background_width
  343. glyph_string_containing_background_width
  344. compute_overhangs_and_x
  345. draw_glyphs
  346. font_for_underline_metrics
  347. append_glyph
  348. append_composite_glyph
  349. take_vertical_position_into_account
  350. produce_image_glyph
  351. produce_xwidget_glyph
  352. append_stretch_glyph
  353. produce_stretch_glyph
  354. produce_special_glyphs
  355. calc_line_height_property
  356. append_glyphless_glyph
  357. produce_glyphless_glyph
  358. gui_produce_glyphs
  359. gui_write_glyphs
  360. gui_insert_glyphs
  361. gui_clear_end_of_line
  362. get_specified_cursor_type
  363. set_frame_cursor_types
  364. get_window_cursor_type
  365. notice_overwritten_cursor
  366. gui_fix_overlapping_area
  367. draw_phys_cursor_glyph
  368. erase_phys_cursor
  369. display_and_set_cursor
  370. update_window_cursor
  371. update_cursor_in_window_tree
  372. gui_update_cursor
  373. gui_clear_cursor
  374. draw_row_with_mouse_face
  375. show_mouse_face
  376. clear_mouse_face
  377. coords_in_mouse_face_p
  378. cursor_in_mouse_face_p
  379. rows_from_pos_range
  380. mouse_face_from_buffer_pos
  381. fast_find_string_pos
  382. mouse_face_from_string_pos
  383. on_hot_spot_p
  384. find_hot_spot
  385. define_frame_cursor1
  386. note_mode_line_or_margin_highlight
  387. note_mouse_highlight
  388. gui_clear_window_mouse_face
  389. cancel_mouse_face
  390. expose_area
  391. expose_line
  392. expose_overlaps
  393. phys_cursor_in_rect_p
  394. gui_draw_vertical_border
  395. gui_draw_right_divider
  396. gui_draw_bottom_divider
  397. expose_window
  398. expose_window_tree
  399. expose_frame
  400. gui_intersect_rectangles
  401. syms_of_xdisp
  402. init_xdisp
  403. show_hourglass
  404. start_hourglass
  405. cancel_hourglass
  406. adjust_glyph_width_for_mouse_face
  407. get_cursor_offset_for_mouse_face

     1 /* Display generation from window structure and buffer text.
     2 
     3 Copyright (C) 1985-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
    21 
    22    Redisplay.
    23 
    24    Emacs separates the task of updating the display from code
    25    modifying global state, e.g. buffer text.  This way functions
    26    operating on buffers don't also have to be concerned with updating
    27    the display.
    28 
    29    Updating the display is triggered by the Lisp interpreter when it
    30    decides it's time to do it.  This is done either automatically for
    31    you as part of the interpreter's command loop or as the result of
    32    calling Lisp functions like `sit-for'.  The C function
    33    `redisplay_internal' in xdisp.c is the only entry into the inner
    34    redisplay code.
    35 
    36    The following diagram shows how redisplay code is invoked.  As you
    37    can see, Lisp calls redisplay and vice versa.
    38 
    39    Under window systems like X, some portions of the redisplay code
    40    are also called asynchronously, due to mouse movement or expose
    41    events.  "Asynchronously" in this context means that any C function
    42    which calls maybe_quit or process_pending_signals could enter
    43    redisplay via expose_frame and/or note_mouse_highlight, if X events
    44    were recently reported to Emacs about mouse movements or frame(s)
    45    that were exposed.  And such redisplay could invoke the Lisp
    46    interpreter, e.g. via the :eval forms in mode-line-format, and as
    47    result the global state could change.  It is therefore very
    48    important that C functions which might cause such "asynchronous"
    49    redisplay, but cannot tolerate the results, use
    50    block_input/unblock_input around code fragments which assume that
    51    global Lisp state doesn't change.  If you don't follow this rule,
    52    you will encounter bugs which are very hard to explain.  One place
    53    that needs to take such precautions is timer_check, some of whose
    54    code cannot tolerate changes in timer alists while it processes
    55    timers.
    56 
    57    +--------------+   redisplay     +----------------+
    58    | Lisp machine |---------------->| Redisplay code |<--+
    59    +--------------+   (xdisp.c)     +----------------+   |
    60           ^                                  |           |
    61           +----------------------------------+           |
    62             Block input to prevent this when             |
    63             called asynchronously!                       |
    64                                                          |
    65                     note_mouse_highlight (asynchronous)  |
    66                                                          |
    67                                     X mouse events  -----+
    68                                                          |
    69                             expose_frame (asynchronous)  |
    70                                                          |
    71                                    X expose events  -----+
    72 
    73    What does redisplay do?  Obviously, it has to figure out somehow what
    74    has been changed since the last time the display has been updated,
    75    and to make these changes visible.  Preferably it would do that in
    76    a moderately intelligent way, i.e. fast.
    77 
    78    Changes in buffer text can be deduced from window and buffer
    79    structures, and from some global variables like `beg_unchanged' and
    80    `end_unchanged'.  The contents of the display are additionally
    81    recorded in a `glyph matrix', a two-dimensional matrix of glyph
    82    structures.  Each row in such a matrix corresponds to a line on the
    83    display, and each glyph in a row corresponds to a column displaying
    84    a character, an image, or what else.  This matrix is called the
    85    `current glyph matrix' or `current matrix' in redisplay
    86    terminology.
    87 
    88    For buffer parts that have been changed since the last update, a
    89    second glyph matrix is constructed, the so called `desired glyph
    90    matrix' or short `desired matrix'.  Current and desired matrix are
    91    then compared to find a cheap way to update the display, e.g. by
    92    reusing part of the display by scrolling lines.  The actual update
    93    of the display of each window by comparing the desired and the
    94    current matrix is done by `update_window', which calls functions
    95    which draw to the glass (those functions are specific to the type
    96    of the window's frame: X, w32, NS, etc.).
    97 
    98    Once the display of a window on the glass has been updated, its
    99    desired matrix is used to update the corresponding rows of the
   100    current matrix, and then the desired matrix is discarded.
   101 
   102    You will find a lot of redisplay optimizations when you start
   103    looking at the innards of redisplay.  The overall goal of all these
   104    optimizations is to make redisplay fast because it is done
   105    frequently.  Some of these optimizations are implemented by the
   106    following functions:
   107 
   108     . try_cursor_movement
   109 
   110       This function tries to update the display if the text in the
   111       window did not change and did not scroll, only point moved, and
   112       it did not move off the displayed portion of the text.
   113 
   114     . try_window_reusing_current_matrix
   115 
   116       This function reuses the current matrix of a window when text
   117       has not changed, but the window start changed (e.g., due to
   118       scrolling).
   119 
   120     . try_window_id
   121 
   122       This function attempts to redisplay a window by reusing parts of
   123       its existing display.  It finds and reuses the part that was not
   124       changed, and redraws the rest.  (The "id" part in the function's
   125       name stands for "insert/delete", not for "identification" or
   126       somesuch.)
   127 
   128     . try_window
   129 
   130       This function performs the full, unoptimized, redisplay of a
   131       single window assuming that its fonts were not changed and that
   132       the cursor will not end up in the scroll margins.  (Loading
   133       fonts requires re-adjustment of dimensions of glyph matrices,
   134       which makes this method impossible to use.)
   135 
   136    The optimizations are tried in sequence (some can be skipped if
   137    it is known that they are not applicable).  If none of the
   138    optimizations were successful, redisplay calls redisplay_windows,
   139    which performs a full redisplay of all windows.
   140 
   141    Note that there's one more important optimization up Emacs's
   142    sleeve, but it is related to actually redrawing the potentially
   143    changed portions of the window/frame, not to reproducing the
   144    desired matrices of those potentially changed portions.  Namely,
   145    the function update_frame and its subroutines, which you will find
   146    in dispnew.c, compare the desired matrices with the current
   147    matrices, and only redraw the portions that changed.  So it could
   148    happen that the functions in this file for some reason decide that
   149    the entire desired matrix needs to be regenerated from scratch, and
   150    still only parts of the Emacs display, or even nothing at all, will
   151    be actually delivered to the glass, because update_frame has found
   152    that the new and the old screen contents are similar or identical.
   153 
   154    Desired matrices.
   155 
   156    Desired matrices are always built per Emacs window.  It is
   157    important to know that a desired matrix is in general "sparse": it
   158    only has some of the glyph rows "enabled".  This is because
   159    redisplay tries to optimize its work, and thus only generates
   160    glyphs for rows that need to be updated on the screen.  Rows that
   161    don't need to be updated are left "disabled", and their contents
   162    should be ignored.
   163 
   164    The function `display_line' is the central function to look at if
   165    you are interested in how the rows of the desired matrix are
   166    produced.  It constructs one row in a desired matrix given an
   167    iterator structure containing both a buffer position and a
   168    description of the environment in which the text is to be
   169    displayed.  But this is too early, read on.
   170 
   171    Glyph rows.
   172 
   173    A glyph row is an array of `struct glyph', where each glyph element
   174    describes a "display element" to be shown on the screen.  More
   175    accurately, a glyph row can have up to 3 different arrays of
   176    glyphs: one each for every display margins, and one for the "text
   177    area", where buffer text is displayed.  The text-area glyph array
   178    is always present, whereas the arrays for the marginal areas are
   179    present (non-empty) only if the corresponding display margin is
   180    shown in the window.  If the glyph array for a marginal area is not
   181    present its beginning and end coincide, i.e. such arrays are
   182    actually empty (they contain no glyphs).  Frame glyph matrices, used
   183    on text-mode terminals (see below) never have marginal areas; they
   184    treat the entire frame-wide row of glyphs as a single large "text
   185    area".
   186 
   187    Iteration over buffer and strings.
   188 
   189    Characters and pixmaps displayed for a range of buffer text depend
   190    on various settings of buffers and windows, on overlays and text
   191    properties, on display tables, on selective display.  The good news
   192    is that all this hairy stuff is hidden behind a small set of
   193    interface functions taking an iterator structure (`struct it')
   194    argument.
   195 
   196    Iteration over things to be displayed is then simple.  It is
   197    started by initializing an iterator with a call to `init_iterator',
   198    passing it the buffer position where to start iteration.  For
   199    iteration over strings, pass -1 as the position to `init_iterator',
   200    and call `reseat_to_string' when the string is ready, to initialize
   201    the iterator for that string.  Thereafter, calls to
   202    `get_next_display_element' fill the iterator structure with
   203    relevant information about the next thing to display.  Calls to
   204    `set_iterator_to_next' move the iterator to the next thing.
   205 
   206    Besides this, an iterator also contains information about the
   207    display environment in which glyphs for display elements are to be
   208    produced.  It has fields for the width and height of the display,
   209    the information whether long lines are truncated or continued, a
   210    current X and Y position, the face currently in effect, and lots of
   211    other stuff you can better see in dispextern.h.
   212 
   213    The "stop position".
   214 
   215    Some of the fields maintained by the iterator change relatively
   216    infrequently.  These include the face of the characters, whether
   217    text is invisible, the object (buffer or display or overlay string)
   218    being iterated, character composition info, etc.  For any given
   219    buffer or string position, the sources of information that affects
   220    the display can be determined by calling the appropriate
   221    primitives, such as `Fnext_single_property_change', but both these
   222    calls and the processing of their return values is relatively
   223    expensive.  To optimize redisplay, the display engine checks these
   224    sources of display information only when needed, not for every
   225    character.  To that end, it always maintains the position of the
   226    next place where it must stop and re-examine all those potential
   227    sources.  This is called "the stop position" and is stored in the
   228    `stop_charpos' field of the iterator.  The stop position is updated
   229    by `compute_stop_pos', which is called whenever the iteration
   230    reaches the current stop position and processes it.  Processing a
   231    stop position is done by `handle_stop', which invokes a series of
   232    handlers, one each for every potential source of display-related
   233    information; see the `it_props' array for those handlers.  For
   234    example, one handler is `handle_face_prop', which detects changes
   235    in face properties, and supplies the face ID that the iterator will
   236    use for all the glyphs it generates up to the next stop position;
   237    this face ID is the result of "realizing" the face specified by the
   238    relevant text properties at this position (see xfaces.c).  Each
   239    handler called by `handle_stop' processes the sources of display
   240    information for which it is "responsible", and returns a value
   241    which tells `handle_stop' what to do next.
   242 
   243    Once `handle_stop' returns, the information it stores in the
   244    iterator fields will not be refreshed until the iteration reaches
   245    the next stop position, which is computed by `compute_stop_pos'
   246    called at the end of `handle_stop'.  `compute_stop_pos' examines
   247    the buffer's or string's interval tree to determine where the text
   248    properties change, finds the next position where overlays and
   249    character composition can change, and stores in `stop_charpos' the
   250    closest position where any of these factors should be reconsidered.
   251 
   252    Handling of the stop position is done as part of the code in
   253    `get_next_display_element'.
   254 
   255    Producing glyphs.
   256 
   257    Glyphs in a desired matrix are normally constructed in a loop
   258    calling `get_next_display_element' and then `PRODUCE_GLYPHS'.  The
   259    call to `PRODUCE_GLYPHS' will fill the iterator structure with
   260    pixel information about the element being displayed and at the same
   261    time will produce glyphs for it.  If the display element fits on
   262    the line being displayed, `set_iterator_to_next' is called next,
   263    otherwise the glyphs produced are discarded, and `display_line'
   264    marks this glyph row as a "continued line".  The function
   265    `display_line' is the workhorse of filling glyph rows in the
   266    desired matrix with glyphs.  In addition to producing glyphs, it
   267    also handles line truncation and continuation, word wrap, and
   268    cursor positioning (for the latter, see `set_cursor_from_row').
   269 
   270    Frame matrices.
   271 
   272    That just couldn't be all, could it?  What about terminal types not
   273    supporting operations on sub-windows of the screen (a.k.a. "TTY" or
   274    "text-mode terminals")?  To update the display on such a terminal,
   275    window-based glyph matrices are not well suited.  To be able to
   276    reuse part of the display (scrolling lines up and down), we must
   277    instead have a view of the whole screen.  This is what `frame
   278    matrices' are for.  They are a trick.
   279 
   280    Frames on text terminals have a glyph pool.  Windows on such a
   281    frame sub-allocate their glyph memory from their frame's glyph
   282    pool.  The frame itself is given its own glyph matrices.  By
   283    coincidence---or maybe something else---rows in window glyph
   284    matrices are slices of corresponding rows in frame matrices.  Thus
   285    writing to window matrices implicitly updates a frame matrix which
   286    provides us with the view of the whole screen that we originally
   287    wanted to have without having to move many bytes around.  Then
   288    updating all the visible windows on text-terminal frames is done by
   289    using the frame matrices, which allows frame-global optimization of
   290    what is actually written to the glass.
   291 
   292    Frame matrices don't have marginal areas, only a text area.  That
   293    is, the entire row of glyphs that spans the width of a text-mode
   294    frame is treated as a single large "text area" for the purposes of
   295    manipulating and updating a frame glyph matrix.
   296 
   297    To be honest, there is a little bit more done for frame matrices,
   298    but not much more.  If you plan to extend that code, take a look at
   299    dispnew.c.  The function build_frame_matrix is a good starting
   300    point.
   301 
   302    Simulating display.
   303 
   304    Some of Emacs commands and functions need to take display layout
   305    into consideration.  For example, C-n moves to the next screen
   306    line, but to implement that, Emacs needs to find the buffer
   307    position which is directly below the cursor position on display.
   308    This is not trivial when buffer display includes variable-size
   309    elements such as different fonts, tall images, etc.
   310 
   311    To solve this problem, the display engine implements several
   312    functions that can move through buffer text in the same manner as
   313    `display_line' and `display_string' do, but without producing any
   314    glyphs for the glyph matrices.  The workhorse of this is
   315    `move_it_in_display_line_to'.  Its code and logic are very similar
   316    to `display_line', but it differs in two important aspects: it
   317    doesn't produce glyphs for any glyph matrix, and it returns a
   318    status telling the caller how it ended the iteration: whether it
   319    reached the required position, hit the end of line, arrived at the
   320    window edge without exhausting the buffer's line, etc.  Since the
   321    glyphs are not produced, the layout information available to the
   322    callers of this function is what is recorded in `struct it' by the
   323    iteration process.
   324 
   325    Several higher-level functions call `move_it_in_display_line_to' to
   326    perform more complex tasks: `move_it_by_lines' can move N lines up
   327    or down from a given buffer position and `move_it_to' can move to a
   328    given buffer position or to a given X or Y pixel coordinate.
   329 
   330    These functions are called by the display engine itself as well,
   331    when it needs to make layout decisions before producing the glyphs.
   332    For example, one of the first things to decide when redisplaying a
   333    window is where to put the `window-start' position; if the window
   334    is to be recentered (the default), Emacs makes that decision by
   335    starting from the position of point, then moving up the number of
   336    lines corresponding to half the window height using
   337    `move_it_by_lines'.
   338 
   339    Bidirectional display.
   340 
   341    Bidirectional display adds quite some hair to this already complex
   342    design.  The good news are that a large portion of that hairy stuff
   343    is hidden in bidi.c behind only 3 interfaces.  bidi.c implements a
   344    reordering engine which is called by `set_iterator_to_next' and
   345    returns the next character to display in the visual order.  See
   346    commentary on bidi.c for more details.  As far as redisplay is
   347    concerned, the effect of calling `bidi_move_to_visually_next', the
   348    main interface of the reordering engine, is that the iterator gets
   349    magically placed on the buffer or string position that is to be
   350    displayed next in the visual order.  In other words, a linear
   351    iteration through the buffer/string is replaced with a non-linear
   352    one.  All the rest of the redisplay is oblivious to the bidi
   353    reordering.
   354 
   355    Well, almost oblivious---there are still complications, most of
   356    them due to the fact that buffer and string positions no longer
   357    change monotonously with glyph indices in a glyph row.  Moreover,
   358    for continued lines, the buffer positions may not even be
   359    monotonously changing with vertical positions.  Also, accounting
   360    for face changes, overlays, etc. becomes more complex because
   361    non-linear iteration could potentially skip many positions with
   362    such changes, and then cross them again on the way back (see
   363    `handle_stop_backwards')...
   364 
   365    One other prominent effect of bidirectional display is that some
   366    paragraphs of text need to be displayed starting at the right
   367    margin of the window---the so-called right-to-left, or R2L
   368    paragraphs.  R2L paragraphs are displayed with R2L glyph rows,
   369    which have their `reversed_p' flag set.  The bidi reordering engine
   370    produces characters in such rows starting from the character which
   371    should be the rightmost on display.  `PRODUCE_GLYPHS' then reverses
   372    the order, when it fills up the glyph row whose `reversed_p' flag
   373    is set, by prepending each new glyph to what is already there,
   374    instead of appending it.  When the glyph row is complete, the
   375    function `extend_face_to_end_of_line' fills the empty space to the
   376    left of the leftmost character with special glyphs, which will
   377    display as, well, empty.  On text terminals, these special glyphs
   378    are simply blank characters.  On graphics terminals, there's a
   379    single stretch glyph of a suitably computed width.  Both the blanks
   380    and the stretch glyph are given the face of the background of the
   381    line.  This way, the terminal-specific back-end can still draw the
   382    glyphs left to right, even for R2L lines.
   383 
   384    Bidirectional display and character compositions.
   385 
   386    Some scripts cannot be displayed by drawing each character
   387    individually, because adjacent characters change each other's shape
   388    on display.  For example, Arabic and Indic scripts belong to this
   389    category.
   390 
   391    Emacs display supports this by providing "character compositions",
   392    most of which is implemented in composite.c.  During the buffer
   393    scan that delivers characters to `PRODUCE_GLYPHS', if the next
   394    character to be delivered is a composed character, the iteration
   395    calls `composition_reseat_it' and `next_element_from_composition'.
   396    If they succeed to compose the character with one or more of the
   397    following characters, the whole sequence of characters that were
   398    composed is recorded in the `struct composition_it' object that is
   399    part of the buffer iterator.  The composed sequence could produce
   400    one or more font glyphs (called "grapheme clusters") on the screen.
   401    Each of these grapheme clusters is then delivered to
   402    `PRODUCE_GLYPHS' in the direction corresponding to the current bidi
   403    scan direction (recorded in the `scan_dir' member of the `struct
   404    bidi_it' object that is part of the iterator).  In particular, if
   405    the bidi iterator currently scans the buffer backwards, the
   406    grapheme clusters are delivered back to front.  This reorders the
   407    grapheme clusters as appropriate for the current bidi context.
   408    Note that this means that the grapheme clusters are always stored
   409    in the `LGSTRING' object (see composite.c) in the logical order.
   410 
   411    Moving an iterator in bidirectional text
   412    without producing glyphs.
   413 
   414    Note one important detail mentioned above: that the bidi reordering
   415    engine, driven by the iterator, produces characters in R2L rows
   416    starting at the character that will be the rightmost on display.
   417    As far as the iterator is concerned, the geometry of such rows is
   418    still left to right, i.e. the iterator "thinks" the first character
   419    is at the leftmost pixel position.  The iterator does not know that
   420    `PRODUCE_GLYPHS' reverses the order of the glyphs that the iterator
   421    delivers.  This is important when functions from the `move_it_*'
   422    family are used to get to certain screen position or to match
   423    screen coordinates with buffer coordinates: these functions use the
   424    iterator geometry, which is left to right even in R2L paragraphs.
   425    This works well with most callers of `move_it_*', because they need
   426    to get to a specific column, and columns are still numbered in the
   427    reading order, i.e. the rightmost character in a R2L paragraph is
   428    still column zero.  But some callers do not get well with this; a
   429    notable example is mouse clicks that need to find the character
   430    that corresponds to certain pixel coordinates.  See
   431    `buffer_posn_from_coords' in dispnew.c for how this is handled.  */
   432 
   433 #include <config.h>
   434 #include <stdlib.h>
   435 #include <limits.h>
   436 #include <math.h>
   437 
   438 #include "lisp.h"
   439 #include "atimer.h"
   440 #include "composite.h"
   441 #include "keyboard.h"
   442 #include "sysstdio.h"
   443 #include "systime.h"
   444 #include "frame.h"
   445 #include "window.h"
   446 #include "termchar.h"
   447 #include "dispextern.h"
   448 #include "character.h"
   449 #include "category.h"
   450 #include "buffer.h"
   451 #include "charset.h"
   452 #include "indent.h"
   453 #include "commands.h"
   454 #include "keymap.h"
   455 #include "disptab.h"
   456 #include "termhooks.h"
   457 #include "termopts.h"
   458 #include "intervals.h"
   459 #include "coding.h"
   460 #include "region-cache.h"
   461 #include "font.h"
   462 #include "fontset.h"
   463 #include "blockinput.h"
   464 #include "xwidget.h"
   465 #ifdef HAVE_WINDOW_SYSTEM
   466 #include TERM_HEADER
   467 #endif /* HAVE_WINDOW_SYSTEM */
   468 
   469 #ifndef FRAME_OUTPUT_DATA
   470 #define FRAME_OUTPUT_DATA(f) (NULL)
   471 #endif
   472 
   473 #define DISP_INFINITY 10000000
   474 
   475 /* Holds the list (error).  */
   476 static Lisp_Object list_of_error;
   477 
   478 #ifdef HAVE_WINDOW_SYSTEM
   479 
   480 /* Test if overflow newline into fringe.  Called with iterator IT
   481    at or past right window margin, and with IT->current_x set.  */
   482 
   483 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT)             \
   484   (!NILP (Voverflow_newline_into_fringe)                \
   485    && FRAME_WINDOW_P ((IT)->f)                          \
   486    && ((IT)->bidi_it.paragraph_dir == R2L               \
   487        ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0)       \
   488        : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0))     \
   489    && (IT)->current_x == (IT)->last_visible_x)
   490 
   491 #else /* !HAVE_WINDOW_SYSTEM */
   492 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
   493 #endif /* HAVE_WINDOW_SYSTEM */
   494 
   495 /* Test if the display element loaded in IT, or the underlying buffer
   496    or string character, is a space or a TAB character.  This is used
   497    to determine where word wrapping can occur.  */
   498 
   499 #define IT_DISPLAYING_WHITESPACE(it)                                    \
   500   ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))        \
   501    || ((STRINGP (it->string)                                            \
   502         && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' '           \
   503             || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t'))     \
   504        || (it->s                                                        \
   505            && (it->s[IT_BYTEPOS (*it)] == ' '                           \
   506                || it->s[IT_BYTEPOS (*it)] == '\t'))                     \
   507        || (IT_BYTEPOS (*it) < ZV_BYTE                                   \
   508            && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' '                 \
   509                || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t'))))
   510 
   511 /* These are the category sets we use.  They are defined by
   512    kinsoku.el and characters.el.  */
   513 #define NOT_AT_EOL '<'
   514 #define NOT_AT_BOL '>'
   515 #define LINE_BREAKABLE '|'
   516 
   517 static bool
   518 it_char_has_category(struct it *it, int cat)
   519 {
   520   int ch = 0;
   521   if (it->what == IT_CHARACTER)
   522     ch = it->c;
   523   else if (STRINGP (it->string))
   524     ch = SREF (it->string, IT_STRING_BYTEPOS (*it));
   525   else if (it->s)
   526     ch = it->s[IT_BYTEPOS (*it)];
   527   else if (IT_BYTEPOS (*it) < ZV_BYTE)
   528     ch = *BYTE_POS_ADDR (IT_BYTEPOS (*it));
   529 
   530   if (ch == 0)
   531     return false;
   532   else
   533     return CHAR_HAS_CATEGORY (ch, cat);
   534 }
   535 
   536 /* Return true if the current character allows wrapping before it.   */
   537 static bool
   538 char_can_wrap_before (struct it *it)
   539 {
   540   if (!word_wrap_by_category)
   541     return !IT_DISPLAYING_WHITESPACE (it);
   542 
   543   /* For CJK (LTR) text in RTL paragraph, EOL and BOL are flipped.
   544      Because in RTL paragraph, each glyph is prepended to the last
   545      one, effectively drawing right to left.  */
   546   int not_at_bol;
   547   if (it->glyph_row && it->glyph_row->reversed_p)
   548     not_at_bol = NOT_AT_EOL;
   549   else
   550     not_at_bol = NOT_AT_BOL;
   551   /* You cannot wrap before a space or tab because that way you'll
   552      have space and tab at the beginning of next line.  */
   553   return (!IT_DISPLAYING_WHITESPACE (it)
   554           /* Can be at BOL.  */
   555           && !it_char_has_category (it, not_at_bol));
   556 }
   557 
   558 /* Return true if the current character allows wrapping after it.   */
   559 static bool
   560 char_can_wrap_after (struct it *it)
   561 {
   562   if (!word_wrap_by_category)
   563     return IT_DISPLAYING_WHITESPACE (it);
   564 
   565   /* For CJK (LTR) text in RTL paragraph, EOL and BOL are flipped.
   566      Because in RTL paragraph, each glyph is prepended to the last
   567      one, effectively drawing right to left.  */
   568   int not_at_eol;
   569   if (it->glyph_row && it->glyph_row->reversed_p)
   570     not_at_eol = NOT_AT_BOL;
   571   else
   572     not_at_eol = NOT_AT_EOL;
   573 
   574   return (IT_DISPLAYING_WHITESPACE (it)
   575           /* Can break after && can be at EOL.  */
   576           || (it_char_has_category (it, LINE_BREAKABLE)
   577               && !it_char_has_category (it, not_at_eol)));
   578 }
   579 
   580 #undef IT_DISPLAYING_WHITESPACE
   581 #undef NOT_AT_EOL
   582 #undef NOT_AT_BOL
   583 #undef LINE_BREAKABLE
   584 
   585 /* If all the conditions needed to print the fill column indicator are
   586    met, return the (nonnegative) column number, else return a negative
   587    value.  */
   588 static int
   589 fill_column_indicator_column (struct it *it, int char_width)
   590 {
   591   if (display_fill_column_indicator
   592       && !it->w->pseudo_window_p
   593       && it->continuation_lines_width == 0
   594       && CHARACTERP (Vdisplay_fill_column_indicator_character))
   595     {
   596       Lisp_Object col = (EQ (Vdisplay_fill_column_indicator_column, Qt)
   597                          ? BVAR (current_buffer, fill_column)
   598                          : Vdisplay_fill_column_indicator_column);
   599 
   600       /* The stretch width needs to consider the latter
   601          added glyph in append_space_for_newline.  */
   602       if (RANGED_FIXNUMP (0, col, INT_MAX))
   603         {
   604           int icol = XFIXNUM (col);
   605           if (!INT_MULTIPLY_WRAPV (char_width, icol, &icol)
   606               && !INT_ADD_WRAPV (it->lnum_pixel_width, icol, &icol))
   607             return icol;
   608         }
   609     }
   610   return -1;
   611 }
   612 
   613 /* True means print newline to stdout before next mini-buffer message.  */
   614 
   615 bool noninteractive_need_newline;
   616 
   617 /* True means print newline to message log before next message.  */
   618 
   619 static bool message_log_need_newline;
   620 
   621 /* Three markers that message_dolog uses.
   622    It could allocate them itself, but that causes trouble
   623    in handling memory-full errors.  */
   624 static Lisp_Object message_dolog_marker1;
   625 static Lisp_Object message_dolog_marker2;
   626 static Lisp_Object message_dolog_marker3;
   627 
   628 /* The buffer position of the first character appearing entirely or
   629    partially on the line of the selected window which contains the
   630    cursor; <= 0 if not known.  Set by set_cursor_from_row, used for
   631    redisplay optimization in redisplay_internal.  */
   632 
   633 static struct text_pos this_line_start_pos;
   634 
   635 /* Number of characters past the end of the line above, including the
   636    terminating newline.  */
   637 
   638 static struct text_pos this_line_end_pos;
   639 
   640 /* The vertical positions and the height of this line.  */
   641 
   642 static int this_line_vpos;
   643 static int this_line_y;
   644 static int this_line_pixel_height;
   645 
   646 /* X position at which this display line starts.  Usually zero;
   647    negative if first character is partially visible.  */
   648 
   649 static int this_line_start_x;
   650 
   651 /* The smallest character position seen by move_it_* functions as they
   652    move across display lines.  Used to set MATRIX_ROW_START_CHARPOS of
   653    hscrolled lines, see display_line.  */
   654 
   655 static struct text_pos this_line_min_pos;
   656 
   657 /* Buffer that this_line_.* variables are referring to.  */
   658 
   659 static struct buffer *this_line_buffer;
   660 
   661 /* True if an overlay arrow has been displayed in this window.  */
   662 
   663 static bool overlay_arrow_seen;
   664 
   665 /* Vector containing glyphs for an ellipsis `...'.  */
   666 
   667 static Lisp_Object default_invis_vector[3];
   668 
   669 /* This is the window where the echo area message was displayed.  It
   670    is always a mini-buffer window, but it may not be the same window
   671    currently active as a mini-buffer.  */
   672 
   673 Lisp_Object echo_area_window;
   674 
   675 /* Stack of messages, which are pushed by push_message and popped and
   676    displayed by restore_message.  */
   677 
   678 static Lisp_Object Vmessage_stack;
   679 
   680 /* True means multibyte characters were enabled when the echo area
   681    message was specified.  */
   682 
   683 static bool message_enable_multibyte;
   684 
   685 /* At each redisplay cycle, we should refresh everything there is to refresh.
   686    To do that efficiently, we use many optimizations that try to make sure we
   687    don't waste too much time updating things that haven't changed.
   688    The coarsest such optimization is that, in the most common cases, we only
   689    look at the selected-window.
   690 
   691    To know whether other windows should be considered for redisplay, we use the
   692    variable windows_or_buffers_changed: as long as it is 0, it means that we
   693    have not noticed anything that should require updating anything else than
   694    the selected-window.  If it is set to REDISPLAY_SOME, it means that since
   695    last redisplay, some changes have been made which could impact other
   696    windows.  To know which ones need redisplay, every buffer, window, and frame
   697    has a `redisplay' bit, which (if true) means that this object needs to be
   698    redisplayed.  If windows_or_buffers_changed is 0, we know there's no point
   699    looking for those `redisplay' bits (actually, there might be some such bits
   700    set, but then only on objects which aren't displayed anyway).
   701 
   702    OTOH if it's non-zero we will have to loop through all windows and then
   703    check the `redisplay' bit of the corresponding window, frame, and buffer, in
   704    order to decide whether that window needs attention or not.  Note that we
   705    can't just look at the frame's redisplay bit to decide that the whole frame
   706    can be skipped, since even if the frame's redisplay bit is unset, some of
   707    its windows's redisplay bits may be set.
   708 
   709    Mostly for historical reasons, windows_or_buffers_changed can also take
   710    other non-zero values.  In that case, the precise value doesn't matter (it
   711    encodes the cause of the setting but is only used for debugging purposes),
   712    and what it means is that we shouldn't pay attention to any `redisplay' bits
   713    and we should simply try and redisplay every window out there.  */
   714 
   715 int windows_or_buffers_changed;
   716 
   717 /* Nonzero if we should redraw the mode lines on the next redisplay.
   718    Similarly to `windows_or_buffers_changed', if it has value REDISPLAY_SOME,
   719    then only redisplay the mode lines in those buffers/windows/frames where the
   720    `redisplay' bit has been set.
   721    For any other value, redisplay all mode lines (the number used is then only
   722    used to track down the cause for this full-redisplay).
   723 
   724    Since the frame title uses the same %-constructs as the mode line
   725    (except %c, %C, and %l), if this variable is non-zero, we also consider
   726    redisplaying the title of each frame, see gui_consider_frame_title.
   727 
   728    The `redisplay' bits are the same as those used for
   729    windows_or_buffers_changed, and setting windows_or_buffers_changed also
   730    causes recomputation of the mode lines of all those windows.  IOW this
   731    variable only has an effect if windows_or_buffers_changed is zero, in which
   732    case we should only need to redisplay the mode-line of those objects with
   733    a `redisplay' bit set but not the window's text content (tho we may still
   734    need to refresh the text content of the selected-window).  */
   735 
   736 int update_mode_lines;
   737 
   738 /* True after display_mode_line if %l was used and it displayed a
   739    line number.  */
   740 
   741 static bool line_number_displayed;
   742 
   743 /* Current, index 0, and last displayed echo area message.  Either
   744    buffers from echo_buffers, or nil to indicate no message.  */
   745 
   746 Lisp_Object echo_area_buffer[2];
   747 
   748 /* The buffers referenced from echo_area_buffer.  */
   749 
   750 static Lisp_Object echo_buffer[2];
   751 
   752 /* A vector saved used in with_area_buffer to reduce consing.  */
   753 
   754 static Lisp_Object Vwith_echo_area_save_vector;
   755 
   756 /* True means display_echo_area should display the last echo area
   757    message again.  Set by redisplay_preserve_echo_area.  */
   758 
   759 static bool display_last_displayed_message_p;
   760 
   761 /* True if echo area is being used by print; false if being used by
   762    message.  */
   763 
   764 static bool message_buf_print;
   765 
   766 /* Set to true in clear_message to make redisplay_internal aware
   767    of an emptied echo area.  */
   768 
   769 static bool message_cleared_p;
   770 
   771 /* A scratch glyph row with contents used for generating truncation
   772    glyphs and overlay-arrow glyphs.  */
   773 
   774 #define MAX_SCRATCH_GLYPHS 100
   775 static struct glyph_row scratch_glyph_row;
   776 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
   777 
   778 /* Ascent and height of the last line processed by move_it_to.  */
   779 
   780 static int last_height;
   781 
   782 /* True if there's a help-echo in the echo area.  */
   783 
   784 bool help_echo_showing_p;
   785 
   786 /* The maximum distance to look ahead for text properties.  Values
   787    that are too small let us call compute_char_face and similar
   788    functions too often which is expensive.  Values that are too large
   789    let us call compute_char_face and alike too often because we
   790    might not be interested in text properties that far away.  */
   791 
   792 #define TEXT_PROP_DISTANCE_LIMIT 100
   793 
   794 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
   795    iterator state and later restore it.  This is needed because the
   796    bidi iterator on bidi.c keeps a stacked cache of its states, which
   797    is really a singleton.  When we use scratch iterator objects to
   798    move around the buffer, we can cause the bidi cache to be pushed or
   799    popped, and therefore we need to restore the cache state when we
   800    return to the original iterator.  */
   801 #define SAVE_IT(ITCOPY, ITORIG, CACHE)          \
   802   do {                                          \
   803     if (CACHE)                                  \
   804       bidi_unshelve_cache (CACHE, true);        \
   805     ITCOPY = ITORIG;                            \
   806     CACHE = bidi_shelve_cache ();               \
   807   } while (false)
   808 
   809 #define RESTORE_IT(pITORIG, pITCOPY, CACHE)     \
   810   do {                                          \
   811     if (pITORIG != pITCOPY)                     \
   812       *(pITORIG) = *(pITCOPY);                  \
   813     bidi_unshelve_cache (CACHE, false);         \
   814     CACHE = NULL;                               \
   815   } while (false)
   816 
   817 /* Functions to mark elements as needing redisplay.  */
   818 enum { REDISPLAY_SOME = 2};     /* Arbitrary choice.  */
   819 
   820 static bool calc_pixel_width_or_height (double *, struct it *, Lisp_Object,
   821                                         struct font *, bool, int *);
   822 
   823 void
   824 redisplay_other_windows (void)
   825 {
   826   if (!windows_or_buffers_changed)
   827     windows_or_buffers_changed = REDISPLAY_SOME;
   828 }
   829 
   830 void
   831 wset_redisplay (struct window *w)
   832 {
   833   /* Beware: selected_window can be nil during early stages.  */
   834   if (!BASE_EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
   835     redisplay_other_windows ();
   836   w->redisplay = true;
   837 }
   838 
   839 void
   840 fset_redisplay (struct frame *f)
   841 {
   842   redisplay_other_windows ();
   843   f->redisplay = true;
   844 }
   845 
   846 void
   847 bset_redisplay (struct buffer *b)
   848 {
   849   int count = buffer_window_count (b);
   850   if (count > 0)
   851     {
   852       /* ... it's visible in other window than selected,  */
   853       if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
   854         redisplay_other_windows ();
   855       /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
   856          so that if we later set windows_or_buffers_changed, this buffer will
   857          not be omitted.  */
   858       b->text->redisplay = true;
   859     }
   860 }
   861 
   862 void
   863 bset_update_mode_line (struct buffer *b)
   864 {
   865   if (!update_mode_lines)
   866     update_mode_lines = REDISPLAY_SOME;
   867   b->text->redisplay = true;
   868 }
   869 
   870 void
   871 wset_update_mode_line (struct window *w)
   872 {
   873   w->update_mode_line = true;
   874   /* When a window's mode line needs to be updated, the window's frame's
   875      title may also need to be updated, but we don't need to worry about it
   876      here.  Instead, `gui_consider_frame_title' is automatically called
   877      whenever w->update_mode_line is set for that frame's selected window.
   878      But for this to work reliably, we have to make sure the window
   879      is considered, so we have to mark it for redisplay.  */
   880   wset_redisplay (w);
   881 }
   882 
   883 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
   884        Sset_buffer_redisplay, 4, 4, 0,
   885        doc: /* Mark the current buffer for redisplay.
   886 This function may be passed to `add-variable-watcher'.  */)
   887   (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
   888 {
   889   bset_update_mode_line (current_buffer);
   890   current_buffer->prevent_redisplay_optimizations_p = true;
   891   return Qnil;
   892 }
   893 
   894 /* redisplay_trace is for displaying traces of redisplay.
   895    If Emacs was compiled with GLYPH_DEBUG defined, the variable
   896    trace_redisplay_p can be set to a non-zero value in debugging
   897    sessions to activate traces.  */
   898 #ifdef GLYPH_DEBUG
   899 extern bool trace_redisplay_p EXTERNALLY_VISIBLE;
   900 bool trace_redisplay_p;
   901 #else
   902 enum { trace_redisplay_p = false };
   903 #endif
   904 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
   905 redisplay_trace (char const *fmt, ...)
   906 {
   907   if (trace_redisplay_p)
   908     {
   909       va_list ap;
   910       va_start (ap, fmt);
   911       vprintf (fmt, ap);
   912       va_end (ap);
   913     }
   914 }
   915 
   916 #ifdef DEBUG_TRACE_MOVE
   917 extern bool trace_move EXTERNALLY_VISIBLE;
   918 bool trace_move;
   919 #else
   920 enum { trace_move = false };
   921 #endif
   922 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
   923 move_trace (char const *fmt, ...)
   924 {
   925   if (trace_move)
   926     {
   927       va_list ap;
   928       va_start (ap, fmt);
   929       vprintf (fmt, ap);
   930       va_end (ap);
   931     }
   932 }
   933 
   934 /* Buffer being redisplayed -- for redisplay_window_error.  */
   935 
   936 static struct buffer *displayed_buffer;
   937 
   938 /* Value returned from text property handlers (see below).  */
   939 
   940 enum prop_handled
   941 {
   942   HANDLED_NORMALLY,
   943   HANDLED_RECOMPUTE_PROPS,
   944   HANDLED_OVERLAY_STRING_CONSUMED,
   945   HANDLED_RETURN
   946 };
   947 
   948 /* A description of text properties that redisplay is interested
   949    in.  */
   950 
   951 struct props
   952 {
   953   /* The symbol index of the name of the property.  */
   954   short name;
   955 
   956   /* A unique index for the property.  */
   957   enum prop_idx idx;
   958 
   959   /* A handler function called to set up iterator IT from the property
   960      at IT's current position.  Value is used to steer handle_stop.  */
   961   enum prop_handled (*handler) (struct it *it);
   962 };
   963 
   964 static enum prop_handled handle_face_prop (struct it *);
   965 static enum prop_handled handle_invisible_prop (struct it *);
   966 static enum prop_handled handle_display_prop (struct it *);
   967 static enum prop_handled handle_composition_prop (struct it *);
   968 static enum prop_handled handle_overlay_change (struct it *);
   969 static enum prop_handled handle_fontified_prop (struct it *);
   970 
   971 /* Properties handled by iterators.  */
   972 
   973 static struct props it_props[] =
   974 {
   975   {SYMBOL_INDEX (Qfontified),   FONTIFIED_PROP_IDX,     handle_fontified_prop},
   976   /* Handle `face' before `display' because some sub-properties of
   977      `display' need to know the face.  */
   978   {SYMBOL_INDEX (Qface),        FACE_PROP_IDX,          handle_face_prop},
   979   {SYMBOL_INDEX (Qdisplay),     DISPLAY_PROP_IDX,       handle_display_prop},
   980   {SYMBOL_INDEX (Qinvisible),   INVISIBLE_PROP_IDX,     handle_invisible_prop},
   981   {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
   982   {0,                           0,                      NULL}
   983 };
   984 
   985 /* Enumeration returned by some move_it_.* functions internally.  */
   986 
   987 enum move_it_result
   988 {
   989   /* Not used.  Undefined value.  */
   990   MOVE_UNDEFINED,
   991 
   992   /* Move ended at the requested buffer position or ZV.  */
   993   MOVE_POS_MATCH_OR_ZV,
   994 
   995   /* Move ended at the requested X pixel position.  */
   996   MOVE_X_REACHED,
   997 
   998   /* Move within a line ended at the end of a line that must be
   999      continued.  */
  1000   MOVE_LINE_CONTINUED,
  1001 
  1002   /* Move within a line ended at the end of a line that would
  1003      be displayed truncated.  */
  1004   MOVE_LINE_TRUNCATED,
  1005 
  1006   /* Move within a line ended at a line end.  */
  1007   MOVE_NEWLINE_OR_CR
  1008 };
  1009 
  1010 /* This counter is used to clear the face cache every once in a while
  1011    in redisplay_internal.  It is incremented for each redisplay.
  1012    Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
  1013    cleared.  */
  1014 
  1015 #define CLEAR_FACE_CACHE_COUNT  500
  1016 static int clear_face_cache_count;
  1017 
  1018 /* Similarly for the image cache.  */
  1019 
  1020 #ifdef HAVE_WINDOW_SYSTEM
  1021 #define CLEAR_IMAGE_CACHE_COUNT 101
  1022 static int clear_image_cache_count;
  1023 
  1024 /* Null glyph slice */
  1025 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
  1026 #endif
  1027 
  1028 /* True while redisplay_internal is in progress.  */
  1029 
  1030 bool redisplaying_p;
  1031 
  1032 /* True while some display-engine code is working on layout of some
  1033    window.
  1034 
  1035    WARNING: Use sparingly, preferably only in top level of commands
  1036    and important functions, because using it in nested calls might
  1037    reset the flag when the inner call returns, behind the back of
  1038    the callers.  */
  1039 bool display_working_on_window_p;
  1040 
  1041 /* If a string, XTread_socket generates an event to display that string.
  1042    (The display is done in read_char.)  */
  1043 
  1044 Lisp_Object help_echo_string;
  1045 Lisp_Object help_echo_window;
  1046 Lisp_Object help_echo_object;
  1047 ptrdiff_t help_echo_pos;
  1048 
  1049 /* Temporary variable for XTread_socket.  */
  1050 
  1051 Lisp_Object previous_help_echo_string;
  1052 
  1053 /* Platform-independent portion of hourglass implementation.  */
  1054 
  1055 #ifdef HAVE_WINDOW_SYSTEM
  1056 
  1057 /* True means an hourglass cursor is currently shown.  */
  1058 static bool hourglass_shown_p;
  1059 
  1060 /* If non-null, an asynchronous timer that, when it expires, displays
  1061    an hourglass cursor on all frames.  */
  1062 static struct atimer *hourglass_atimer;
  1063 
  1064 #endif /* HAVE_WINDOW_SYSTEM */
  1065 
  1066 /* Default number of seconds to wait before displaying an hourglass
  1067    cursor.  */
  1068 #define DEFAULT_HOURGLASS_DELAY 1
  1069 
  1070 #ifdef HAVE_WINDOW_SYSTEM
  1071 
  1072 /* Default pixel width of `thin-space' display method.  */
  1073 #define THIN_SPACE_WIDTH 1
  1074 
  1075 #endif /* HAVE_WINDOW_SYSTEM */
  1076 
  1077 /* Function prototypes.  */
  1078 
  1079 static void setup_for_ellipsis (struct it *, int);
  1080 static void set_iterator_to_next (struct it *, bool);
  1081 static void mark_window_display_accurate_1 (struct window *, bool);
  1082 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
  1083 static bool cursor_row_p (struct glyph_row *);
  1084 static int redisplay_mode_lines (Lisp_Object, bool);
  1085 
  1086 static void handle_line_prefix (struct it *);
  1087 
  1088 static void handle_stop_backwards (struct it *, ptrdiff_t);
  1089 static void unwind_with_echo_area_buffer (Lisp_Object);
  1090 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
  1091 static bool current_message_1 (void *, Lisp_Object);
  1092 static bool truncate_message_1 (void *, Lisp_Object);
  1093 static void set_message (Lisp_Object);
  1094 static bool set_message_1 (void *, Lisp_Object);
  1095 static bool display_echo_area_1 (void *, Lisp_Object);
  1096 static bool resize_mini_window_1 (void *, Lisp_Object);
  1097 static void unwind_redisplay (void);
  1098 static void extend_face_to_end_of_line (struct it *);
  1099 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
  1100 static void push_it (struct it *, struct text_pos *);
  1101 static void iterate_out_of_display_property (struct it *);
  1102 static void pop_it (struct it *);
  1103 static void redisplay_internal (void);
  1104 static void echo_area_display (bool);
  1105 static void block_buffer_flips (void);
  1106 static void unblock_buffer_flips (void);
  1107 static void redisplay_windows (Lisp_Object);
  1108 static void redisplay_window (Lisp_Object, bool);
  1109 static Lisp_Object redisplay_window_error (Lisp_Object);
  1110 static Lisp_Object redisplay_window_0 (Lisp_Object);
  1111 static Lisp_Object redisplay_window_1 (Lisp_Object);
  1112 static bool set_cursor_from_row (struct window *, struct glyph_row *,
  1113                                  struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
  1114                                  int, int);
  1115 static bool cursor_row_fully_visible_p (struct window *, bool, bool, bool);
  1116 static bool update_menu_bar (struct frame *, bool, bool);
  1117 static bool try_window_reusing_current_matrix (struct window *);
  1118 static int try_window_id (struct window *);
  1119 static void maybe_produce_line_number (struct it *);
  1120 static bool should_produce_line_number (struct it *);
  1121 static bool display_line (struct it *, int);
  1122 static int display_mode_lines (struct window *);
  1123 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
  1124 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
  1125                                  Lisp_Object, bool);
  1126 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
  1127                                    Lisp_Object);
  1128 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
  1129 static void display_menu_bar (struct window *);
  1130 static void display_tab_bar (struct window *);
  1131 static void update_tab_bar (struct frame *, bool);
  1132 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
  1133                                       ptrdiff_t *);
  1134 static void pint2str (register char *, register int, register ptrdiff_t);
  1135 
  1136 static int display_string (const char *, Lisp_Object, Lisp_Object,
  1137                            ptrdiff_t, ptrdiff_t, struct it *, int, int, int,
  1138                            int);
  1139 static void compute_line_metrics (struct it *);
  1140 static bool get_overlay_strings (struct it *, ptrdiff_t);
  1141 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
  1142 static void next_overlay_string (struct it *);
  1143 static void reseat (struct it *, struct text_pos, bool);
  1144 static void reseat_1 (struct it *, struct text_pos, bool);
  1145 static bool next_element_from_display_vector (struct it *);
  1146 static bool next_element_from_string (struct it *);
  1147 static bool next_element_from_c_string (struct it *);
  1148 static bool next_element_from_buffer (struct it *);
  1149 static bool next_element_from_composition (struct it *);
  1150 static bool next_element_from_image (struct it *);
  1151 static bool next_element_from_stretch (struct it *);
  1152 static bool next_element_from_xwidget (struct it *);
  1153 static void load_overlay_strings (struct it *, ptrdiff_t);
  1154 static bool get_next_display_element (struct it *);
  1155 static enum move_it_result
  1156        move_it_in_display_line_to (struct it *, ptrdiff_t, int,
  1157                                    enum move_operation_enum);
  1158 static void get_visually_first_element (struct it *);
  1159 static void compute_stop_pos (struct it *);
  1160 static int face_before_or_after_it_pos (struct it *, bool);
  1161 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
  1162                                 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
  1163 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
  1164                                        Lisp_Object, struct text_pos *,
  1165                                        ptrdiff_t, int, bool, bool);
  1166 static int underlying_face_id (const struct it *);
  1167 
  1168 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
  1169 #define face_after_it_pos(IT)  face_before_or_after_it_pos (IT, false)
  1170 
  1171 #ifdef HAVE_WINDOW_SYSTEM
  1172 
  1173 static void update_tool_bar (struct frame *, bool);
  1174 static void gui_draw_bottom_divider (struct window *w);
  1175 static void notice_overwritten_cursor (struct window *,
  1176                                        enum glyph_row_area,
  1177                                        int, int, int, int);
  1178 static int  normal_char_height (struct font *, int);
  1179 static void normal_char_ascent_descent (struct font *, int, int *, int *);
  1180 
  1181 static void append_stretch_glyph (struct it *, Lisp_Object,
  1182                                   int, int, int);
  1183 
  1184 static Lisp_Object get_it_property (struct it *, Lisp_Object);
  1185 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
  1186                                               struct font *, int, bool);
  1187 static int adjust_glyph_width_for_mouse_face (struct glyph *,
  1188                                               struct glyph_row *,
  1189                                               struct window *, struct face *,
  1190                                               struct face *);
  1191 static void get_cursor_offset_for_mouse_face (struct window *w,
  1192                                               struct glyph_row *row,
  1193                                               int *offset);
  1194 #endif /* HAVE_WINDOW_SYSTEM */
  1195 
  1196 static void produce_special_glyphs (struct it *, enum display_element_type);
  1197 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
  1198 static bool coords_in_mouse_face_p (struct window *, int, int);
  1199 static void reset_box_start_end_flags (struct it *);
  1200 
  1201 
  1202 
  1203 /***********************************************************************
  1204                       Window display dimensions
  1205  ***********************************************************************/
  1206 
  1207 /* Return the bottom boundary y-position for text lines in window W.
  1208    This is the first y position at which a line cannot start.
  1209    It is relative to the top of the window.
  1210 
  1211    This is the height of W minus the height of a mode line, if any.  */
  1212 
  1213 int
  1214 window_text_bottom_y (struct window *w)
  1215 {
  1216   int height = WINDOW_PIXEL_HEIGHT (w);
  1217 
  1218   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1219 
  1220   if (window_wants_mode_line (w))
  1221     height -= CURRENT_MODE_LINE_HEIGHT (w);
  1222 
  1223   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1224 
  1225   return height;
  1226 }
  1227 
  1228 /* Return the pixel width of display area AREA of window W.
  1229    ANY_AREA means return the total width of W, not including
  1230    fringes to the left and right of the window.  */
  1231 
  1232 int
  1233 window_box_width (struct window *w, enum glyph_row_area area)
  1234 {
  1235   int width = w->pixel_width;
  1236 
  1237   if (!w->pseudo_window_p)
  1238     {
  1239       width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  1240       width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
  1241 
  1242       if (area == TEXT_AREA)
  1243         width -= (WINDOW_MARGINS_WIDTH (w)
  1244                    + WINDOW_FRINGES_WIDTH (w));
  1245       else if (area == LEFT_MARGIN_AREA)
  1246         width = WINDOW_LEFT_MARGIN_WIDTH (w);
  1247       else if (area == RIGHT_MARGIN_AREA)
  1248         width = WINDOW_RIGHT_MARGIN_WIDTH (w);
  1249     }
  1250 
  1251   /* With wide margins, fringes, etc. we might end up with a negative
  1252      width, correct that here.  */
  1253   return max (0, width);
  1254 }
  1255 
  1256 
  1257 /* Return the pixel height of the display area of window W, not
  1258    including mode lines of W, if any.  */
  1259 
  1260 int
  1261 window_box_height (struct window *w)
  1262 {
  1263   struct frame *f = XFRAME (w->frame);
  1264   int height = WINDOW_PIXEL_HEIGHT (w);
  1265 
  1266   eassert (height >= 0);
  1267 
  1268   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1269   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1270 
  1271   /* Note: the code below that determines the mode-line/header-line/tab-line
  1272      height is essentially the same as that contained in the macro
  1273      CURRENT_{MODE,HEADER,TAB}_LINE_HEIGHT, except that it checks whether
  1274      the appropriate glyph row has its `mode_line_p' flag set, and if
  1275      it doesn't, uses estimate_mode_line_height instead.  */
  1276 
  1277   if (window_wants_mode_line (w))
  1278     {
  1279       if (w->mode_line_height >= 0)
  1280         height -= w->mode_line_height;
  1281       else
  1282         {
  1283           struct glyph_row *ml_row
  1284             = (w->current_matrix && w->current_matrix->rows
  1285                ? MATRIX_MODE_LINE_ROW (w->current_matrix)
  1286                : 0);
  1287           if (ml_row && ml_row->mode_line_p)
  1288             height -= ml_row->height;
  1289           else
  1290             height -= estimate_mode_line_height
  1291               (f, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w));
  1292         }
  1293     }
  1294 
  1295   if (window_wants_tab_line (w))
  1296     {
  1297       if (w->tab_line_height >= 0)
  1298         height -= w->tab_line_height;
  1299       else
  1300         {
  1301           struct glyph_row *tl_row
  1302             = (w->current_matrix && w->current_matrix->rows
  1303                ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  1304                : 0);
  1305           if (tl_row && tl_row->mode_line_p)
  1306             height -= tl_row->height;
  1307           else
  1308             height -= estimate_mode_line_height (f, TAB_LINE_FACE_ID);
  1309         }
  1310     }
  1311 
  1312   if (window_wants_header_line (w))
  1313     {
  1314       if (w->header_line_height >= 0)
  1315         height -= w->header_line_height;
  1316       else
  1317         {
  1318           struct glyph_row *hl_row
  1319             = (w->current_matrix && w->current_matrix->rows
  1320                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  1321                : 0);
  1322           if (hl_row && hl_row->mode_line_p)
  1323             height -= hl_row->height;
  1324           else
  1325             height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
  1326         }
  1327     }
  1328 
  1329   /* With a very small font and a mode-line that's taller than
  1330      default, we might end up with a negative height.  */
  1331   return max (0, height);
  1332 }
  1333 
  1334 /* Return the window-relative coordinate of the left edge of display
  1335    area AREA of window W.  ANY_AREA means return the left edge of the
  1336    whole window, to the right of the left fringe of W.  */
  1337 
  1338 int
  1339 window_box_left_offset (struct window *w, enum glyph_row_area area)
  1340 {
  1341   int x;
  1342 
  1343   if (w->pseudo_window_p)
  1344     return 0;
  1345 
  1346   x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
  1347 
  1348   if (area == TEXT_AREA)
  1349     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1350           + window_box_width (w, LEFT_MARGIN_AREA));
  1351   else if (area == RIGHT_MARGIN_AREA)
  1352     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1353           + window_box_width (w, LEFT_MARGIN_AREA)
  1354           + window_box_width (w, TEXT_AREA)
  1355           + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  1356              ? 0
  1357              : WINDOW_RIGHT_FRINGE_WIDTH (w)));
  1358   else if (area == LEFT_MARGIN_AREA
  1359            && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
  1360     x += WINDOW_LEFT_FRINGE_WIDTH (w);
  1361 
  1362   /* Don't return more than the window's pixel width.  */
  1363   return min (x, w->pixel_width);
  1364 }
  1365 
  1366 
  1367 /* Return the window-relative coordinate of the right edge of display
  1368    area AREA of window W.  ANY_AREA means return the right edge of the
  1369    whole window, to the left of the right fringe of W.  */
  1370 
  1371 static int
  1372 window_box_right_offset (struct window *w, enum glyph_row_area area)
  1373 {
  1374   /* Don't return more than the window's pixel width.  */
  1375   return min (window_box_left_offset (w, area) + window_box_width (w, area),
  1376               w->pixel_width);
  1377 }
  1378 
  1379 /* Return the frame-relative coordinate of the left edge of display
  1380    area AREA of window W.  ANY_AREA means return the left edge of the
  1381    whole window, to the right of the left fringe of W.  */
  1382 
  1383 int
  1384 window_box_left (struct window *w, enum glyph_row_area area)
  1385 {
  1386   struct frame *f = XFRAME (w->frame);
  1387   int x;
  1388 
  1389   if (w->pseudo_window_p)
  1390     return FRAME_INTERNAL_BORDER_WIDTH (f);
  1391 
  1392   x = (WINDOW_LEFT_EDGE_X (w)
  1393        + window_box_left_offset (w, area));
  1394 
  1395   return x;
  1396 }
  1397 
  1398 
  1399 /* Return the frame-relative coordinate of the right edge of display
  1400    area AREA of window W.  ANY_AREA means return the right edge of the
  1401    whole window, to the left of the right fringe of W.  */
  1402 
  1403 int
  1404 window_box_right (struct window *w, enum glyph_row_area area)
  1405 {
  1406   return window_box_left (w, area) + window_box_width (w, area);
  1407 }
  1408 
  1409 /* Get the bounding box of the display area AREA of window W, without
  1410    mode lines, in frame-relative coordinates.  ANY_AREA means the
  1411    whole window, not including the left and right fringes of
  1412    the window.  Return in *BOX_X and *BOX_Y the frame-relative pixel
  1413    coordinates of the upper-left corner of the box.  Return in
  1414    *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box.  */
  1415 
  1416 void
  1417 window_box (struct window *w, enum glyph_row_area area, int *box_x,
  1418             int *box_y, int *box_width, int *box_height)
  1419 {
  1420   if (box_width)
  1421     *box_width = window_box_width (w, area);
  1422   if (box_height)
  1423     *box_height = window_box_height (w);
  1424   if (box_x)
  1425     *box_x = window_box_left (w, area);
  1426   if (box_y)
  1427     {
  1428       *box_y = WINDOW_TOP_EDGE_Y (w);
  1429       if (window_wants_tab_line (w))
  1430         *box_y += CURRENT_TAB_LINE_HEIGHT (w);
  1431       if (window_wants_header_line (w))
  1432         *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
  1433     }
  1434 }
  1435 
  1436 #ifdef HAVE_WINDOW_SYSTEM
  1437 
  1438 /* Get the bounding box of the display area AREA of window W, without
  1439    mode lines and both fringes of the window.  Return in *TOP_LEFT_X
  1440    and TOP_LEFT_Y the frame-relative pixel coordinates of the
  1441    upper-left corner of the box.  Return in *BOTTOM_RIGHT_X, and
  1442    *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
  1443    box.  */
  1444 
  1445 static void
  1446 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
  1447                   int *bottom_right_x, int *bottom_right_y)
  1448 {
  1449   window_box (w, ANY_AREA, top_left_x, top_left_y,
  1450               bottom_right_x, bottom_right_y);
  1451   *bottom_right_x += *top_left_x;
  1452   *bottom_right_y += *top_left_y;
  1453 }
  1454 
  1455 #endif /* HAVE_WINDOW_SYSTEM */
  1456 
  1457 /***********************************************************************
  1458                               Utilities
  1459  ***********************************************************************/
  1460 
  1461 /* Return the bottom y-position of the line the iterator IT is in.
  1462    This can modify IT's settings.  */
  1463 
  1464 int
  1465 line_bottom_y (struct it *it)
  1466 {
  1467   int line_height = it->max_ascent + it->max_descent;
  1468   int line_top_y = it->current_y;
  1469 
  1470   if (line_height == 0)
  1471     {
  1472       if (last_height)
  1473         line_height = last_height;
  1474       else if (IT_CHARPOS (*it) < ZV)
  1475         {
  1476           move_it_by_lines (it, 1);
  1477           line_height = (it->max_ascent || it->max_descent
  1478                          ? it->max_ascent + it->max_descent
  1479                          : last_height);
  1480         }
  1481       else
  1482         {
  1483           struct glyph_row *row = it->glyph_row;
  1484 
  1485           /* Use the default character height.  */
  1486           it->glyph_row = NULL;
  1487           it->what = IT_CHARACTER;
  1488           it->c = ' ';
  1489           it->len = 1;
  1490           PRODUCE_GLYPHS (it);
  1491           line_height = it->ascent + it->descent;
  1492           it->glyph_row = row;
  1493         }
  1494     }
  1495 
  1496   return line_top_y + line_height;
  1497 }
  1498 
  1499 DEFUN ("line-pixel-height", Fline_pixel_height,
  1500        Sline_pixel_height, 0, 0, 0,
  1501        doc: /* Return height in pixels of text line in the selected window.
  1502 
  1503 Value is the height in pixels of the line at point.  */)
  1504   (void)
  1505 {
  1506   struct it it;
  1507   struct text_pos pt;
  1508   struct window *w = XWINDOW (selected_window);
  1509   struct buffer *old_buffer = NULL;
  1510   Lisp_Object result;
  1511 
  1512   if (XBUFFER (w->contents) != current_buffer)
  1513     {
  1514       old_buffer = current_buffer;
  1515       set_buffer_internal_1 (XBUFFER (w->contents));
  1516     }
  1517   SET_TEXT_POS (pt, PT, PT_BYTE);
  1518   void *itdata = bidi_shelve_cache ();
  1519   start_display (&it, w, pt);
  1520   /* Start from the beginning of the screen line, to make sure we
  1521      traverse all of its display elements, and thus capture the
  1522      correct metrics.  */
  1523   move_it_by_lines (&it, 0);
  1524   it.vpos = it.current_y = 0;
  1525   last_height = 0;
  1526   result = make_fixnum (line_bottom_y (&it));
  1527   if (old_buffer)
  1528     set_buffer_internal_1 (old_buffer);
  1529 
  1530   bidi_unshelve_cache (itdata, false);
  1531   return result;
  1532 }
  1533 
  1534 /* Return the default pixel height of text lines in window W.  The
  1535    value is the canonical height of the W frame's default font, plus
  1536    any extra space required by the line-spacing variable or frame
  1537    parameter.
  1538 
  1539    Implementation note: this ignores any line-spacing text properties
  1540    put on the newline characters.  This is because those properties
  1541    only affect the _screen_ line ending in the newline (i.e., in a
  1542    continued line, only the last screen line will be affected), which
  1543    means only a small number of lines in a buffer can ever use this
  1544    feature.  Since this function is used to compute the default pixel
  1545    equivalent of text lines in a window, we can safely ignore those
  1546    few lines.  For the same reasons, we ignore the line-height
  1547    properties.  */
  1548 int
  1549 default_line_pixel_height (struct window *w)
  1550 {
  1551   struct frame *f = WINDOW_XFRAME (w);
  1552   int height = FRAME_LINE_HEIGHT (f);
  1553 
  1554   if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
  1555     {
  1556       struct buffer *b = XBUFFER (w->contents);
  1557       Lisp_Object val = BVAR (b, extra_line_spacing);
  1558 
  1559       if (NILP (val))
  1560         val = BVAR (&buffer_defaults, extra_line_spacing);
  1561       if (!NILP (val))
  1562         {
  1563           if (RANGED_FIXNUMP (0, val, INT_MAX))
  1564             height += XFIXNAT (val);
  1565           else if (FLOATP (val))
  1566             {
  1567               int addon = XFLOAT_DATA (val) * height + 0.5;
  1568 
  1569               if (addon >= 0)
  1570                 height += addon;
  1571             }
  1572         }
  1573       else
  1574         height += f->extra_line_spacing;
  1575     }
  1576 
  1577   return height;
  1578 }
  1579 
  1580 /* Subroutine of pos_visible_p below.  Extracts a display string, if
  1581    any, from the display spec given as its argument.  */
  1582 static Lisp_Object
  1583 string_from_display_spec (Lisp_Object spec)
  1584 {
  1585   if (VECTORP (spec))
  1586     {
  1587       for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
  1588         if (STRINGP (AREF (spec, i)))
  1589           return AREF (spec, i);
  1590     }
  1591   else
  1592     {
  1593       for (; CONSP (spec); spec = XCDR (spec))
  1594         if (STRINGP (XCAR (spec)))
  1595           return XCAR (spec);
  1596     }
  1597   return spec;
  1598 }
  1599 
  1600 
  1601 /* Limit insanely large values of W->hscroll on frame F to the largest
  1602    value that will still prevent first_visible_x and last_visible_x of
  1603    'struct it' from overflowing an int.  */
  1604 static int
  1605 window_hscroll_limited (struct window *w, struct frame *f)
  1606 {
  1607   ptrdiff_t window_hscroll = w->hscroll;
  1608   int window_text_width = window_box_width (w, TEXT_AREA);
  1609   int colwidth = FRAME_COLUMN_WIDTH (f);
  1610 
  1611   if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
  1612     window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
  1613 
  1614   return window_hscroll;
  1615 }
  1616 
  1617 /* Reset the box-face start and end flags in the iterator.  This is
  1618    called after producing glyphs, such that we reset these flags only
  1619    after producing a glyph with the flag set.  */
  1620 
  1621 static void
  1622 reset_box_start_end_flags (struct it *it)
  1623 {
  1624   /* Don't reset if we've drawn the glyph in the display margins --
  1625      those don't count as "produced glyphs".  */
  1626   if (it->area == TEXT_AREA
  1627       /* Don't reset if we displayed a fringe bitmap.  */
  1628       && !(it->what == IT_IMAGE && it->image_id < 0))
  1629     {
  1630       /* Don't reset if the face is not a box face: that might mean we
  1631          are iterating some overlay or display string, and the first
  1632          character to have the box face is yet to be seen, when we pop
  1633          the iterator stack. */
  1634       if (it->face_box_p)
  1635         it->start_of_box_run_p = false;
  1636       it->end_of_box_run_p = false;
  1637     }
  1638 }
  1639 
  1640 /* Return true if position CHARPOS is visible in window W.
  1641    CHARPOS < 0 means return info about WINDOW_END position.
  1642    If visible, set *X and *Y to pixel coordinates of top left corner.
  1643    Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
  1644    Set *ROWH and *VPOS to row's visible height and VPOS (row number).  */
  1645 
  1646 bool
  1647 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
  1648                int *rtop, int *rbot, int *rowh, int *vpos)
  1649 {
  1650   struct it it;
  1651   void *itdata = bidi_shelve_cache ();
  1652   struct text_pos top;
  1653   bool visible_p = false;
  1654   struct buffer *old_buffer = NULL;
  1655   bool r2l = false;
  1656 
  1657   if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
  1658     return visible_p;
  1659 
  1660   if (XBUFFER (w->contents) != current_buffer)
  1661     {
  1662       old_buffer = current_buffer;
  1663       set_buffer_internal_1 (XBUFFER (w->contents));
  1664     }
  1665 
  1666   SET_TEXT_POS_FROM_MARKER (top, w->start);
  1667   /* Scrolling a minibuffer window via scroll bar when the echo area
  1668      shows long text sometimes resets the minibuffer contents behind
  1669      our backs.  Also, someone might narrow-to-region and immediately
  1670      call a scroll function.  */
  1671   if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
  1672     SET_TEXT_POS (top, BEGV, BEGV_BYTE);
  1673 
  1674   /* If the top of the window is after CHARPOS, the latter is surely
  1675      not visible.  */
  1676   if (charpos >= 0 && CHARPOS (top) > charpos)
  1677     return visible_p;
  1678 
  1679   /* Some Lisp hook could call us in the middle of redisplaying this
  1680      very window.  If, by some bad luck, we are retrying redisplay
  1681      because we found that the mode-line height and/or tab/header-line
  1682      height needs to be updated, the assignment of mode_line_height
  1683      and header_line_height below could disrupt that, due to the
  1684      selected/nonselected window dance during mode-line display, and
  1685      we could infloop.  Avoid that.  */
  1686   int prev_mode_line_height = w->mode_line_height;
  1687   int prev_header_line_height = w->header_line_height;
  1688   int prev_tab_line_height = w->tab_line_height;
  1689   /* Compute exact mode line heights.  */
  1690   if (window_wants_mode_line (w))
  1691     {
  1692       Lisp_Object window_mode_line_format
  1693         = window_parameter (w, Qmode_line_format);
  1694 
  1695       w->mode_line_height
  1696         = display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
  1697                              NILP (window_mode_line_format)
  1698                              ? BVAR (current_buffer, mode_line_format)
  1699                              : window_mode_line_format);
  1700     }
  1701 
  1702   if (window_wants_tab_line (w))
  1703     {
  1704       Lisp_Object window_tab_line_format
  1705         = window_parameter (w, Qtab_line_format);
  1706 
  1707       w->tab_line_height
  1708         = display_mode_line (w, TAB_LINE_FACE_ID,
  1709                              NILP (window_tab_line_format)
  1710                              ? BVAR (current_buffer, tab_line_format)
  1711                              : window_tab_line_format);
  1712     }
  1713 
  1714   if (window_wants_header_line (w))
  1715     {
  1716       Lisp_Object window_header_line_format
  1717         = window_parameter (w, Qheader_line_format);
  1718 
  1719       w->header_line_height
  1720         = display_mode_line (w, HEADER_LINE_FACE_ID,
  1721                              NILP (window_header_line_format)
  1722                              ? BVAR (current_buffer, header_line_format)
  1723                              : window_header_line_format);
  1724     }
  1725 
  1726   start_display (&it, w, top);
  1727   move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
  1728               (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
  1729 
  1730   /* Adjust for line numbers, if CHARPOS is at or beyond first_visible_x,
  1731      but we didn't yet produce the line-number glyphs.  */
  1732   if (!NILP (Vdisplay_line_numbers)
  1733       && it.current_x >= it.first_visible_x
  1734       && IT_CHARPOS (it) == charpos
  1735       && !it.line_number_produced_p)
  1736     {
  1737       /* If the pixel width of line numbers was not yet known, compute
  1738          it now.  This usually happens in the first display line of a
  1739          window.  */
  1740       if (!it.lnum_pixel_width)
  1741         {
  1742           struct it it2;
  1743           void *it2data = NULL;
  1744 
  1745           SAVE_IT (it2, it, it2data);
  1746           move_it_by_lines (&it, 1);
  1747           it2.lnum_pixel_width = it.lnum_pixel_width;
  1748           RESTORE_IT (&it, &it2, it2data);
  1749         }
  1750       it.current_x += it.lnum_pixel_width;
  1751     }
  1752 
  1753   if (charpos >= 0
  1754       && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
  1755            && IT_CHARPOS (it) >= charpos)
  1756           /* When scanning backwards under bidi iteration, move_it_to
  1757              stops at or _before_ CHARPOS, because it stops at or to
  1758              the _right_ of the character at CHARPOS.  */
  1759           || (it.bidi_p && it.bidi_it.scan_dir == -1
  1760               && IT_CHARPOS (it) <= charpos)))
  1761     {
  1762       /* We have reached CHARPOS, or passed it.  How the call to
  1763          move_it_to can overshoot: (i) If CHARPOS is on invisible text
  1764          or covered by a display property, move_it_to stops at the end
  1765          of the invisible text, to the right of CHARPOS.  (ii) If
  1766          CHARPOS is in a display vector, move_it_to stops on its last
  1767          glyph.  */
  1768       int top_x = it.current_x;
  1769       int top_y = it.current_y;
  1770       int window_top_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  1771       int bottom_y;
  1772       struct it save_it;
  1773       void *save_it_data = NULL;
  1774 
  1775       /* Calling line_bottom_y may change it.method, it.position, etc.  */
  1776       SAVE_IT (save_it, it, save_it_data);
  1777       last_height = 0;
  1778       bottom_y = line_bottom_y (&it);
  1779       if (top_y < window_top_y)
  1780         visible_p = bottom_y > window_top_y;
  1781       else if (top_y < it.last_visible_y)
  1782         visible_p = true;
  1783       if (bottom_y >= it.last_visible_y
  1784           && it.bidi_p && it.bidi_it.scan_dir == -1
  1785           && IT_CHARPOS (it) < charpos)
  1786         {
  1787           /* When the last line of the window is scanned backwards
  1788              under bidi iteration, we could be duped into thinking
  1789              that we have passed CHARPOS, when in fact move_it_to
  1790              simply stopped short of CHARPOS because it reached
  1791              last_visible_y.  To see if that's what happened, we call
  1792              move_it_to again with a slightly larger vertical limit,
  1793              and see if it actually moved vertically; if it did, we
  1794              didn't really reach CHARPOS, which is beyond window end.  */
  1795           /* Why 10? because we don't know how many canonical lines
  1796              will the height of the next line(s) be.  So we guess.  */
  1797           int ten_more_lines = 10 * default_line_pixel_height (w);
  1798 
  1799           move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
  1800                       MOVE_TO_POS | MOVE_TO_Y);
  1801           if (it.current_y > top_y)
  1802             visible_p = false;
  1803 
  1804         }
  1805       RESTORE_IT (&it, &save_it, save_it_data);
  1806       if (visible_p)
  1807         {
  1808           if (it.method == GET_FROM_DISPLAY_VECTOR)
  1809             {
  1810               /* We stopped on the last glyph of a display vector.
  1811                  Try and recompute.  Hack alert!  */
  1812               if (charpos < 2 || top.charpos >= charpos)
  1813                 top_x = it.glyph_row->x;
  1814               else
  1815                 {
  1816                   struct it it2, it2_prev;
  1817                   /* The idea is to get to the previous buffer
  1818                      position, consume the character there, and use
  1819                      the pixel coordinates we get after that.  But if
  1820                      the previous buffer position is also displayed
  1821                      from a display vector, we need to consume all of
  1822                      the glyphs from that display vector.  */
  1823                   start_display (&it2, w, top);
  1824                   it2.glyph_row = NULL;
  1825                   move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
  1826                   /* If we didn't get to CHARPOS - 1, there's some
  1827                      replacing display property at that position, and
  1828                      we stopped after it.  That is exactly the place
  1829                      whose coordinates we want.  */
  1830                   if (IT_CHARPOS (it2) != charpos - 1)
  1831                     it2_prev = it2;
  1832                   else
  1833                     {
  1834                       /* Iterate until we get out of the display
  1835                          vector that displays the character at
  1836                          CHARPOS - 1.  */
  1837                       do {
  1838                         get_next_display_element (&it2);
  1839                         PRODUCE_GLYPHS (&it2);
  1840                         it2_prev = it2;
  1841                         set_iterator_to_next (&it2, true);
  1842                       } while (it2.method == GET_FROM_DISPLAY_VECTOR
  1843                                && IT_CHARPOS (it2) < charpos);
  1844                     }
  1845                   if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
  1846                       || it2_prev.current_x > it2_prev.last_visible_x)
  1847                     top_x = it.glyph_row->x;
  1848                   else
  1849                     {
  1850                       top_x = it2_prev.current_x;
  1851                       top_y = it2_prev.current_y;
  1852                     }
  1853                 }
  1854             }
  1855           else if (IT_CHARPOS (it) != charpos)
  1856             {
  1857               Lisp_Object cpos = make_fixnum (charpos);
  1858               Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  1859               Lisp_Object string = string_from_display_spec (spec);
  1860               struct text_pos tpos;
  1861               bool newline_in_string
  1862                 = (STRINGP (string)
  1863                    && memchr (SDATA (string), '\n', SBYTES (string)));
  1864 
  1865               SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
  1866               bool replacing_spec_p
  1867                 = (!NILP (spec)
  1868                    && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
  1869                                            charpos, FRAME_WINDOW_P (it.f)));
  1870               /* The tricky code below is needed because there's a
  1871                  discrepancy between move_it_to and how we set cursor
  1872                  when PT is at the beginning of a portion of text
  1873                  covered by a display property or an overlay with a
  1874                  display property, or the display line ends in a
  1875                  newline from a display string.  move_it_to will stop
  1876                  _after_ such display strings, whereas
  1877                  set_cursor_from_row conspires with cursor_row_p to
  1878                  place the cursor on the first glyph produced from the
  1879                  display string.  */
  1880 
  1881               /* We have overshoot PT because it is covered by a
  1882                  display property that replaces the text it covers.
  1883                  If the string includes embedded newlines, we are also
  1884                  in the wrong display line.  Backtrack to the correct
  1885                  line, where the display property begins.  */
  1886               if (replacing_spec_p)
  1887                 {
  1888                   Lisp_Object startpos, endpos;
  1889                   EMACS_INT start, end;
  1890                   struct it it3;
  1891 
  1892                   /* Find the first and the last buffer positions
  1893                      covered by the display string.  */
  1894                   endpos =
  1895                     Fnext_single_char_property_change (cpos, Qdisplay,
  1896                                                        Qnil, Qnil);
  1897                   startpos =
  1898                     Fprevious_single_char_property_change (endpos, Qdisplay,
  1899                                                            Qnil, Qnil);
  1900                   start = XFIXNAT (startpos);
  1901                   end = XFIXNAT (endpos);
  1902                   /* Move to the last buffer position before the
  1903                      display property.  */
  1904                   start_display (&it3, w, top);
  1905                   if (start > CHARPOS (top))
  1906                     move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
  1907                   /* Move forward one more line if the position before
  1908                      the display string is a newline or if it is the
  1909                      rightmost character on a line that is
  1910                      continued or word-wrapped.  */
  1911                   if (it3.method == GET_FROM_BUFFER
  1912                       && (it3.c == '\n'
  1913                           || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
  1914                     move_it_by_lines (&it3, 1);
  1915                   else if (move_it_in_display_line_to (&it3, -1,
  1916                                                        it3.current_x
  1917                                                        + it3.pixel_width,
  1918                                                        MOVE_TO_X)
  1919                            == MOVE_LINE_CONTINUED)
  1920                     {
  1921                       move_it_by_lines (&it3, 1);
  1922                       /* When we are under word-wrap, the #$@%!
  1923                          move_it_by_lines moves 2 lines, so we need to
  1924                          fix that up.  */
  1925                       if (it3.line_wrap == WORD_WRAP)
  1926                         move_it_by_lines (&it3, -1);
  1927                     }
  1928 
  1929                   /* Record the vertical coordinate of the display
  1930                      line where we wound up.  */
  1931                   top_y = it3.current_y;
  1932                   if (it3.bidi_p)
  1933                     {
  1934                       /* When characters are reordered for display,
  1935                          the character displayed to the left of the
  1936                          display string could be _after_ the display
  1937                          property in the logical order.  Use the
  1938                          smallest vertical position of these two.  */
  1939                       start_display (&it3, w, top);
  1940                       move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
  1941                       if (it3.current_y < top_y)
  1942                         top_y = it3.current_y;
  1943                     }
  1944                   /* Move from the top of the window to the beginning
  1945                      of the display line where the display string
  1946                      begins.  */
  1947                   start_display (&it3, w, top);
  1948                   it3.glyph_row = NULL;
  1949                   move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
  1950                   /* If it3_moved stays false after the 'while' loop
  1951                      below, that means we already were at a newline
  1952                      before the loop (e.g., the display string begins
  1953                      with a newline), so we don't need to return to
  1954                      the last position before the display string,
  1955                      because PRODUCE_GLYPHS will not produce anything
  1956                      for a newline.  */
  1957                   bool it3_moved = false;
  1958                   int top_x_before_string = it3.current_x;
  1959                   /* Finally, advance the iterator until we hit the
  1960                      first display element whose character position is
  1961                      at or beyond CHARPOS, or until the first newline
  1962                      from the display string, which signals the end of
  1963                      the display line.  */
  1964                   while (get_next_display_element (&it3))
  1965                     {
  1966                       if (!EQ (it3.object, string))
  1967                         top_x_before_string = it3.current_x;
  1968                       PRODUCE_GLYPHS (&it3);
  1969                       if ((it3.bidi_it.scan_dir == 1
  1970                            && IT_CHARPOS (it3) >= charpos)
  1971                           || (it3.bidi_it.scan_dir == -1
  1972                               && IT_CHARPOS (it3) <= charpos)
  1973                           || ITERATOR_AT_END_OF_LINE_P (&it3))
  1974                         break;
  1975                       it3_moved = true;
  1976                       set_iterator_to_next (&it3, false);
  1977                     }
  1978                   top_x = it3.current_x - it3.pixel_width;
  1979                   /* Account for line-number display, if IT3 still
  1980                      didn't.  This can happen if START - 1 is the
  1981                      first or the last character on its display line.  */
  1982                   if (!it3.line_number_produced_p)
  1983                     {
  1984                       if (it3.lnum_pixel_width > 0)
  1985                         {
  1986                           top_x += it3.lnum_pixel_width;
  1987                           top_x_before_string += it3.lnum_pixel_width;
  1988                         }
  1989                       else if (it.line_number_produced_p)
  1990                         {
  1991                           top_x += it.lnum_pixel_width;
  1992                           top_x_before_string += it3.lnum_pixel_width;
  1993                         }
  1994                     }
  1995                   /* Normally, we would exit the above loop because we
  1996                      found the display element whose character
  1997                      position is CHARPOS.  For the contingency that we
  1998                      didn't, and stopped at the first newline from the
  1999                      display string, reset top_x to the coordinate of
  2000                      the rightmost glyph not from the string.  */
  2001                   if (it3_moved
  2002                       && newline_in_string
  2003                       && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
  2004                     top_x = top_x_before_string;
  2005                 }
  2006             }
  2007 
  2008           *x = top_x;
  2009           /* The condition below is a heuristic fix for the situation
  2010              where move_it_to stops just after finishing the display
  2011              of a fringe bitmap, which resets it.ascent to zero, and
  2012              thus causes Y to be offset by it.max_ascent.  */
  2013           if (it.ascent == 0 && it.what == IT_IMAGE
  2014               && it.method != GET_FROM_IMAGE
  2015               && it.image_id < 0
  2016               && it.max_ascent > 0)
  2017             *y = max (top_y, window_top_y);
  2018           else
  2019             *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
  2020           *rtop = max (0, window_top_y - top_y);
  2021           *rbot = max (0, bottom_y - it.last_visible_y);
  2022           *rowh = max (0, (min (bottom_y, it.last_visible_y)
  2023                            - max (top_y, window_top_y)));
  2024           *vpos = it.vpos;
  2025           if (it.bidi_it.paragraph_dir == R2L)
  2026             r2l = true;
  2027         }
  2028     }
  2029   else
  2030     {
  2031       /* Either we were asked to provide info about WINDOW_END, or
  2032          CHARPOS is in the partially visible glyph row at end of
  2033          window.  */
  2034       struct it it2;
  2035       void *it2data = NULL;
  2036 
  2037       SAVE_IT (it2, it, it2data);
  2038       if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
  2039         move_it_by_lines (&it, 1);
  2040       if (charpos < IT_CHARPOS (it)
  2041           || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
  2042         {
  2043           visible_p = true;
  2044           RESTORE_IT (&it2, &it2, it2data);
  2045           move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
  2046           *x = it2.current_x;
  2047           if (it2.ascent == 0 && it2.what == IT_IMAGE
  2048               && it2.method != GET_FROM_IMAGE
  2049               && it2.image_id < 0
  2050               && it2.max_ascent > 0)
  2051             *y = it2.current_y;
  2052           else
  2053             *y = it2.current_y + it2.max_ascent - it2.ascent;
  2054           *rtop = max (0, -it2.current_y);
  2055           *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
  2056                            - it.last_visible_y));
  2057           *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
  2058                                 it.last_visible_y)
  2059                            - max (max (it2.current_y,
  2060                                        WINDOW_TAB_LINE_HEIGHT (w)),
  2061                                   WINDOW_HEADER_LINE_HEIGHT (w))));
  2062           *vpos = it2.vpos;
  2063           if (it2.bidi_it.paragraph_dir == R2L)
  2064             r2l = true;
  2065         }
  2066       else
  2067         bidi_unshelve_cache (it2data, true);
  2068     }
  2069   bidi_unshelve_cache (itdata, false);
  2070 
  2071   if (old_buffer)
  2072     set_buffer_internal_1 (old_buffer);
  2073 
  2074   if (visible_p)
  2075     {
  2076       if (w->hscroll > 0)
  2077         *x -=
  2078           window_hscroll_limited (w, WINDOW_XFRAME (w))
  2079           * WINDOW_FRAME_COLUMN_WIDTH (w);
  2080       /* For lines in an R2L paragraph, we need to mirror the X pixel
  2081          coordinate wrt the text area.  For the reasons, see the
  2082          commentary in buffer_posn_from_coords and the explanation of
  2083          the geometry used by the move_it_* functions at the end of
  2084          the large commentary near the beginning of this file.  */
  2085       if (r2l)
  2086         *x = window_box_width (w, TEXT_AREA) - *x - 1;
  2087     }
  2088 
  2089 #if false
  2090   /* Debugging code.  */
  2091   if (visible_p)
  2092     fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
  2093              charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
  2094   else
  2095     fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
  2096 #endif
  2097 
  2098   /* Restore potentially overwritten values.  */
  2099   w->mode_line_height = prev_mode_line_height;
  2100   w->header_line_height = prev_header_line_height;
  2101   w->tab_line_height = prev_tab_line_height;
  2102 
  2103   return visible_p;
  2104 }
  2105 
  2106 
  2107 /* Return the next character from STR.  Return in *LEN the length of
  2108    the character.  This is like string_char_and_length but never
  2109    returns an invalid character.  If we find one, we return a `?', but
  2110    with the length of the invalid character.  */
  2111 
  2112 static int
  2113 check_char_and_length (const unsigned char *str, int *len)
  2114 {
  2115   int c = string_char_and_length (str, len);
  2116   if (!CHAR_VALID_P (c))
  2117     /* We may not change the length here because other places in Emacs
  2118        don't use this function, i.e. they silently accept invalid
  2119        characters.  */
  2120     c = '?';
  2121 
  2122   return c;
  2123 }
  2124 
  2125 
  2126 
  2127 /* Given a position POS containing a valid character and byte position
  2128    in STRING, return the position NCHARS ahead (NCHARS >= 0).  */
  2129 
  2130 static struct text_pos
  2131 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
  2132 {
  2133   eassert (STRINGP (string) && nchars >= 0);
  2134 
  2135   if (STRING_MULTIBYTE (string))
  2136     {
  2137       const unsigned char *p = SDATA (string) + BYTEPOS (pos);
  2138 
  2139       while (nchars--)
  2140         {
  2141           int len = BYTES_BY_CHAR_HEAD (*p);
  2142           p += len;
  2143           CHARPOS (pos) += 1;
  2144           BYTEPOS (pos) += len;
  2145         }
  2146     }
  2147   else
  2148     SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
  2149 
  2150   return pos;
  2151 }
  2152 
  2153 
  2154 /* Value is the text position, i.e. character and byte position,
  2155    for character position CHARPOS in STRING.  */
  2156 
  2157 static struct text_pos
  2158 string_pos (ptrdiff_t charpos, Lisp_Object string)
  2159 {
  2160   struct text_pos pos;
  2161   eassert (STRINGP (string));
  2162   eassert (charpos >= 0);
  2163   SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
  2164   return pos;
  2165 }
  2166 
  2167 
  2168 /* Value is a text position, i.e. character and byte position, for
  2169    character position CHARPOS in C string S.  MULTIBYTE_P
  2170    means recognize multibyte characters.  */
  2171 
  2172 static struct text_pos
  2173 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
  2174 {
  2175   struct text_pos pos;
  2176 
  2177   eassert (s != NULL);
  2178   eassert (charpos >= 0);
  2179 
  2180   if (multibyte_p)
  2181     {
  2182       SET_TEXT_POS (pos, 0, 0);
  2183       while (charpos--)
  2184         {
  2185           int len = BYTES_BY_CHAR_HEAD (*s);
  2186           s += len;
  2187           CHARPOS (pos) += 1;
  2188           BYTEPOS (pos) += len;
  2189         }
  2190     }
  2191   else
  2192     SET_TEXT_POS (pos, charpos, charpos);
  2193 
  2194   return pos;
  2195 }
  2196 
  2197 
  2198 /* Value is the number of characters in C string S.  MULTIBYTE_P
  2199    means recognize multibyte characters.  */
  2200 
  2201 static ptrdiff_t
  2202 number_of_chars (const char *s, bool multibyte_p)
  2203 {
  2204   ptrdiff_t nchars;
  2205 
  2206   if (multibyte_p)
  2207     {
  2208       ptrdiff_t rest = strlen (s);
  2209       const unsigned char *p = (const unsigned char *) s;
  2210 
  2211       for (nchars = 0; rest > 0; ++nchars)
  2212         {
  2213           int len = BYTES_BY_CHAR_HEAD (*p);
  2214           rest -= len, p += len;
  2215         }
  2216     }
  2217   else
  2218     nchars = strlen (s);
  2219 
  2220   return nchars;
  2221 }
  2222 
  2223 
  2224 /* Compute byte position NEWPOS->bytepos corresponding to
  2225    NEWPOS->charpos.  POS is a known position in string STRING.
  2226    NEWPOS->charpos must be >= POS.charpos.  */
  2227 
  2228 static void
  2229 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
  2230 {
  2231   eassert (STRINGP (string));
  2232   eassert (CHARPOS (*newpos) >= CHARPOS (pos));
  2233 
  2234   if (STRING_MULTIBYTE (string))
  2235     *newpos = string_pos_nchars_ahead (pos, string,
  2236                                        CHARPOS (*newpos) - CHARPOS (pos));
  2237   else
  2238     BYTEPOS (*newpos) = CHARPOS (*newpos);
  2239 }
  2240 
  2241 /* EXPORT:
  2242    Return an estimation of the pixel height of mode or header lines on
  2243    frame F.  FACE_ID specifies what line's height to estimate.  */
  2244 
  2245 int
  2246 estimate_mode_line_height (struct frame *f, enum face_id face_id)
  2247 {
  2248 #ifdef HAVE_WINDOW_SYSTEM
  2249   if (FRAME_WINDOW_P (f))
  2250     {
  2251       int height = FONT_HEIGHT (FRAME_FONT (f));
  2252 
  2253       /* This function is called so early when Emacs starts that the face
  2254          cache and mode line face are not yet initialized.  */
  2255       if (FRAME_FACE_CACHE (f))
  2256         {
  2257           struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
  2258           if (face)
  2259             {
  2260               if (face->font)
  2261                 height = normal_char_height (face->font, -1);
  2262               if (face->box_horizontal_line_width > 0)
  2263                 height += 2 * face->box_horizontal_line_width;
  2264             }
  2265         }
  2266 
  2267       return height;
  2268     }
  2269 #endif
  2270 
  2271   return 1;
  2272 }
  2273 
  2274 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
  2275    coordinates in (*X, *Y).  Set *BOUNDS to the rectangle that the
  2276    glyph at X, Y occupies, if BOUNDS != 0.  If NOCLIP, do
  2277    not force the value into range.  */
  2278 
  2279 void
  2280 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
  2281                        NativeRectangle *bounds, bool noclip)
  2282 {
  2283 
  2284 #ifdef HAVE_WINDOW_SYSTEM
  2285   if (FRAME_WINDOW_P (f))
  2286     {
  2287       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
  2288          even for negative values.  */
  2289       if (pix_x < 0)
  2290         pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
  2291       if (pix_y < 0)
  2292         pix_y -= FRAME_LINE_HEIGHT (f) - 1;
  2293 
  2294       pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
  2295       pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
  2296 
  2297       if (bounds)
  2298         STORE_NATIVE_RECT (*bounds,
  2299                            FRAME_COL_TO_PIXEL_X (f, pix_x),
  2300                            FRAME_LINE_TO_PIXEL_Y (f, pix_y),
  2301                            FRAME_COLUMN_WIDTH (f) - 1,
  2302                            FRAME_LINE_HEIGHT (f) - 1);
  2303 
  2304       /* PXW: Should we clip pixels before converting to columns/lines?  */
  2305       if (!noclip)
  2306         {
  2307           if (pix_x < 0)
  2308             pix_x = 0;
  2309           else if (pix_x > FRAME_TOTAL_COLS (f))
  2310             pix_x = FRAME_TOTAL_COLS (f);
  2311 
  2312           if (pix_y < 0)
  2313             pix_y = 0;
  2314           else if (pix_y > FRAME_TOTAL_LINES (f))
  2315             pix_y = FRAME_TOTAL_LINES (f);
  2316         }
  2317     }
  2318 #endif
  2319 
  2320   *x = pix_x;
  2321   *y = pix_y;
  2322 }
  2323 
  2324 
  2325 /* Find the glyph under window-relative coordinates X/Y in window W.
  2326    Consider only glyphs from buffer text, i.e. no glyphs from overlay
  2327    strings.  Return in *HPOS and *VPOS the row and column number of
  2328    the glyph found.  Return in *AREA the glyph area containing X.
  2329    Value is a pointer to the glyph found or null if X/Y is not on
  2330    text, or we can't tell because W's current matrix is not up to
  2331    date.  */
  2332 
  2333 struct glyph *
  2334 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
  2335                   int *dx, int *dy, int *area)
  2336 {
  2337   struct glyph *glyph, *end;
  2338   struct glyph_row *row = NULL;
  2339   int x0, i;
  2340 
  2341   /* Find row containing Y.  Give up if some row is not enabled.  */
  2342   for (i = 0; i < w->current_matrix->nrows; ++i)
  2343     {
  2344       row = MATRIX_ROW (w->current_matrix, i);
  2345       if (!row->enabled_p)
  2346         return NULL;
  2347       if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
  2348         break;
  2349     }
  2350 
  2351   *vpos = i;
  2352   *hpos = 0;
  2353 
  2354   /* Give up if Y is not in the window.  */
  2355   if (i == w->current_matrix->nrows)
  2356     return NULL;
  2357 
  2358   /* Get the glyph area containing X.  */
  2359   if (w->pseudo_window_p)
  2360     {
  2361       *area = TEXT_AREA;
  2362       x0 = 0;
  2363     }
  2364   else
  2365     {
  2366       if (x < window_box_left_offset (w, TEXT_AREA))
  2367         {
  2368           *area = LEFT_MARGIN_AREA;
  2369           x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
  2370         }
  2371       else if (x < window_box_right_offset (w, TEXT_AREA))
  2372         {
  2373           *area = TEXT_AREA;
  2374           x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
  2375         }
  2376       else
  2377         {
  2378           *area = RIGHT_MARGIN_AREA;
  2379           x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
  2380         }
  2381     }
  2382 
  2383   /* Find glyph containing X.  */
  2384   glyph = row->glyphs[*area];
  2385   end = glyph + row->used[*area];
  2386   x -= x0;
  2387   while (glyph < end && x >= glyph->pixel_width)
  2388     {
  2389       x -= glyph->pixel_width;
  2390       ++glyph;
  2391     }
  2392 
  2393   if (glyph == end)
  2394     return NULL;
  2395 
  2396   if (dx)
  2397     {
  2398       *dx = x;
  2399       *dy = y - (row->y + row->ascent - glyph->ascent);
  2400     }
  2401 
  2402   *hpos = glyph - row->glyphs[*area];
  2403   return glyph;
  2404 }
  2405 
  2406 /* Convert frame-relative x/y to coordinates relative to window W.
  2407    Takes pseudo-windows into account.  */
  2408 
  2409 static void
  2410 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
  2411 {
  2412   if (w->pseudo_window_p)
  2413     {
  2414       /* A pseudo-window is always full-width, and starts at the
  2415          left edge of the frame, plus a frame border.  */
  2416       struct frame *f = XFRAME (w->frame);
  2417       *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
  2418       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2419     }
  2420   else
  2421     {
  2422       *x -= WINDOW_LEFT_EDGE_X (w);
  2423       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2424     }
  2425 }
  2426 
  2427 #ifdef HAVE_WINDOW_SYSTEM
  2428 
  2429 /* EXPORT:
  2430    Return in RECTS[] at most N clipping rectangles for glyph string S.
  2431    Return the number of stored rectangles.  */
  2432 
  2433 int
  2434 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
  2435 {
  2436   Emacs_Rectangle r;
  2437 
  2438   if (n <= 0)
  2439     return 0;
  2440 
  2441   if (s->row->full_width_p)
  2442     {
  2443       /* Draw full-width.  X coordinates are relative to S->w->left_col.  */
  2444       r.x = WINDOW_LEFT_EDGE_X (s->w);
  2445       if (s->row->mode_line_p)
  2446         r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
  2447       else
  2448         r.width = WINDOW_PIXEL_WIDTH (s->w);
  2449 
  2450       /* Unless displaying a mode or menu bar line, which are always
  2451          fully visible, clip to the visible part of the row.  */
  2452       if (s->w->pseudo_window_p)
  2453         r.height = s->row->visible_height;
  2454       else
  2455         r.height = s->height;
  2456     }
  2457   else
  2458     {
  2459       /* This is a text line that may be partially visible.  */
  2460       r.x = window_box_left (s->w, s->area);
  2461       r.width = window_box_width (s->w, s->area);
  2462       r.height = s->row->visible_height;
  2463     }
  2464 
  2465   if (s->clip_head)
  2466     if (r.x < s->clip_head->x)
  2467       {
  2468         if (r.width >= s->clip_head->x - r.x)
  2469           r.width -= s->clip_head->x - r.x;
  2470         else
  2471           r.width = 0;
  2472         r.x = s->clip_head->x;
  2473       }
  2474   if (s->clip_tail)
  2475     if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
  2476       {
  2477         if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
  2478           r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
  2479         else
  2480           r.width = 0;
  2481       }
  2482 
  2483   /* If S draws overlapping rows, it's sufficient to use the top and
  2484      bottom of the window for clipping because this glyph string
  2485      intentionally draws over other lines.  */
  2486   if (s->for_overlaps)
  2487     {
  2488       r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2489       r.height = window_text_bottom_y (s->w) - r.y;
  2490 
  2491       /* Alas, the above simple strategy does not work for the
  2492          environments with anti-aliased text: if the same text is
  2493          drawn onto the same place multiple times, it gets thicker.
  2494          If the overlap we are processing is for the erased cursor, we
  2495          take the intersection with the rectangle of the cursor.  */
  2496       if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
  2497         {
  2498           Emacs_Rectangle rc, r_save = r;
  2499 
  2500           rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
  2501           rc.y = s->w->phys_cursor.y;
  2502           rc.width = s->w->phys_cursor_width;
  2503           rc.height = s->w->phys_cursor_height;
  2504 
  2505           gui_intersect_rectangles (&r_save, &rc, &r);
  2506         }
  2507     }
  2508   else
  2509     {
  2510       /* Don't use S->y for clipping because it doesn't take partially
  2511          visible lines into account.  For example, it can be negative for
  2512          partially visible lines at the top of a window.  */
  2513       if (!s->row->full_width_p
  2514           && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
  2515         r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2516       else
  2517         r.y = max (0, s->row->y);
  2518     }
  2519 
  2520   r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
  2521 
  2522   /* If drawing the cursor, don't let glyph draw outside its
  2523      advertised boundaries. Cleartype does this under some circumstances.  */
  2524   if (s->hl == DRAW_CURSOR)
  2525     {
  2526       struct glyph *glyph = s->first_glyph;
  2527       int height, max_y;
  2528 
  2529       if (s->x > r.x)
  2530         {
  2531           if (r.width >= s->x - r.x)
  2532             r.width -= s->x - r.x;
  2533           else  /* R2L hscrolled row with cursor outside text area */
  2534             r.width = 0;
  2535           r.x = s->x;
  2536         }
  2537       r.width = min (r.width, glyph->pixel_width);
  2538 
  2539       /* If r.y is below window bottom, ensure that we still see a cursor.  */
  2540       height = min (glyph->ascent + glyph->descent,
  2541                     min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
  2542       max_y = window_text_bottom_y (s->w) - height;
  2543       max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
  2544       if (s->ybase - glyph->ascent > max_y)
  2545         {
  2546           r.y = max_y;
  2547           r.height = height;
  2548         }
  2549       else
  2550         {
  2551           /* Don't draw cursor glyph taller than our actual glyph.  */
  2552           height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
  2553           if (height < r.height)
  2554             {
  2555               max_y = r.y + r.height;
  2556               r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
  2557               r.height = min (max_y - r.y, height);
  2558             }
  2559         }
  2560     }
  2561 
  2562   if (s->row->clip)
  2563     {
  2564       Emacs_Rectangle r_save = r;
  2565 
  2566       if (! gui_intersect_rectangles (&r_save, s->row->clip, &r))
  2567         r.width = 0;
  2568     }
  2569 
  2570   if ((s->for_overlaps & OVERLAPS_BOTH) == 0
  2571       || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
  2572     {
  2573 #ifdef CONVERT_FROM_EMACS_RECT
  2574       CONVERT_FROM_EMACS_RECT (r, *rects);
  2575 #else
  2576       *rects = r;
  2577 #endif
  2578       return 1;
  2579     }
  2580   else
  2581     {
  2582       /* If we are processing overlapping and allowed to return
  2583          multiple clipping rectangles, we exclude the row of the glyph
  2584          string from the clipping rectangle.  This is to avoid drawing
  2585          the same text on the environment with anti-aliasing.  */
  2586 #ifdef CONVERT_FROM_EMACS_RECT
  2587       Emacs_Rectangle rs[2];
  2588 #else
  2589       Emacs_Rectangle *rs = rects;
  2590 #endif
  2591       int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
  2592 
  2593       if (s->for_overlaps & OVERLAPS_PRED)
  2594         {
  2595           rs[i] = r;
  2596           if (r.y + r.height > row_y)
  2597             {
  2598               if (r.y < row_y)
  2599                 rs[i].height = row_y - r.y;
  2600               else
  2601                 rs[i].height = 0;
  2602             }
  2603           i++;
  2604         }
  2605       if (s->for_overlaps & OVERLAPS_SUCC)
  2606         {
  2607           rs[i] = r;
  2608           if (r.y < row_y + s->row->visible_height)
  2609             {
  2610               if (r.y + r.height > row_y + s->row->visible_height)
  2611                 {
  2612                   rs[i].y = row_y + s->row->visible_height;
  2613                   rs[i].height = r.y + r.height - rs[i].y;
  2614                 }
  2615               else
  2616                 rs[i].height = 0;
  2617             }
  2618           i++;
  2619         }
  2620 
  2621       n = i;
  2622 #ifdef CONVERT_FROM_EMACS_RECT
  2623       for (i = 0; i < n; i++)
  2624         CONVERT_FROM_EMACS_RECT (rs[i], rects[i]);
  2625 #endif
  2626       return n;
  2627     }
  2628 }
  2629 
  2630 /* EXPORT:
  2631    Return in *NR the clipping rectangle for glyph string S.  */
  2632 
  2633 void
  2634 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
  2635 {
  2636   get_glyph_string_clip_rects (s, nr, 1);
  2637 }
  2638 
  2639 
  2640 /* EXPORT:
  2641    Return the position and height of the phys cursor in window W.
  2642    Set w->phys_cursor_width to width of phys cursor.
  2643 */
  2644 
  2645 void
  2646 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
  2647                           struct glyph *glyph, int *xp, int *yp, int *heightp)
  2648 {
  2649   struct frame *f = XFRAME (WINDOW_FRAME (w));
  2650   int x, y, wd, h, h0, y0, ascent;
  2651 
  2652   /* Compute the width of the rectangle to draw.  If on a stretch
  2653      glyph, and `x-stretch-cursor' is nil, don't draw a rectangle
  2654      as wide as the glyph, but use a canonical character width
  2655      instead.  */
  2656   wd = glyph->pixel_width;
  2657 
  2658   x = w->phys_cursor.x;
  2659   if (x < 0)
  2660     {
  2661       wd += x;
  2662       x = 0;
  2663     }
  2664 
  2665   if (glyph->type == STRETCH_GLYPH
  2666       && !x_stretch_cursor_p)
  2667     wd = min (FRAME_COLUMN_WIDTH (f), wd);
  2668   w->phys_cursor_width = wd;
  2669 
  2670   /* Don't let the hollow cursor glyph descend below the glyph row's
  2671      ascent value, lest the hollow cursor looks funny.  */
  2672   y = w->phys_cursor.y;
  2673   ascent = row->ascent;
  2674   /* The test for row at ZV is for when line numbers are displayed and
  2675      point is at EOB: the cursor could then be smaller or larger than
  2676      the default face's font.  */
  2677   if (!row->ends_at_zv_p && row->ascent < glyph->ascent)
  2678     {
  2679       y -= glyph->ascent - row->ascent;
  2680       ascent = glyph->ascent;
  2681     }
  2682 
  2683   /* If y is below window bottom, ensure that we still see a cursor.  */
  2684   h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
  2685 
  2686   h = max (h0, ascent + glyph->descent);
  2687   /* Don't let the cursor exceed the dimensions of the row, so that
  2688      the upper/lower side of the box aren't clipped.  */
  2689   h = min (h, row->height);
  2690   h0 = min (h0, ascent + glyph->descent);
  2691 
  2692   y0 = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  2693   if (y < y0)
  2694     {
  2695       h = max (h - (y0 - y) + 1, h0);
  2696       y = y0 - 1;
  2697     }
  2698   else
  2699     {
  2700       y0 = window_text_bottom_y (w) - h0;
  2701       if (y > y0)
  2702         {
  2703           h += y - y0;
  2704           y = y0;
  2705         }
  2706     }
  2707 
  2708   *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
  2709   *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
  2710   *heightp = h;
  2711 }
  2712 
  2713 /*
  2714  * Remember which glyph the mouse is over.
  2715  */
  2716 
  2717 void
  2718 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
  2719 {
  2720   Lisp_Object window;
  2721   struct window *w;
  2722   struct glyph_row *r, *gr, *end_row;
  2723   enum window_part part;
  2724   enum glyph_row_area area;
  2725   int x, y, width, height;
  2726 
  2727   if (mouse_fine_grained_tracking)
  2728     {
  2729       STORE_NATIVE_RECT (*rect, gx, gy, 1, 1);
  2730       return;
  2731     }
  2732 
  2733   /* Try to determine frame pixel position and size of the glyph under
  2734      frame pixel coordinates X/Y on frame F.  */
  2735 
  2736   if (window_resize_pixelwise)
  2737     {
  2738       width = height = 1;
  2739       goto virtual_glyph;
  2740     }
  2741   else if (!f->glyphs_initialized_p
  2742            || (window = window_from_coordinates (f, gx, gy, &part, false, false),
  2743                NILP (window)))
  2744     {
  2745       width = FRAME_SMALLEST_CHAR_WIDTH (f);
  2746       height = FRAME_SMALLEST_FONT_HEIGHT (f);
  2747       goto virtual_glyph;
  2748     }
  2749 
  2750   w = XWINDOW (window);
  2751   width = WINDOW_FRAME_COLUMN_WIDTH (w);
  2752   height = WINDOW_FRAME_LINE_HEIGHT (w);
  2753 
  2754   x = window_relative_x_coord (w, part, gx);
  2755   y = gy - WINDOW_TOP_EDGE_Y (w);
  2756 
  2757   r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
  2758   end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
  2759 
  2760   if (w->pseudo_window_p)
  2761     {
  2762       area = TEXT_AREA;
  2763       part = ON_MODE_LINE; /* Don't adjust margin. */
  2764       goto text_glyph;
  2765     }
  2766 
  2767   switch (part)
  2768     {
  2769     case ON_LEFT_MARGIN:
  2770       area = LEFT_MARGIN_AREA;
  2771       goto text_glyph;
  2772 
  2773     case ON_RIGHT_MARGIN:
  2774       area = RIGHT_MARGIN_AREA;
  2775       goto text_glyph;
  2776 
  2777     case ON_TAB_LINE:
  2778     case ON_HEADER_LINE:
  2779     case ON_MODE_LINE:
  2780       gr = (part == ON_TAB_LINE
  2781             ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  2782             : (part == ON_HEADER_LINE
  2783                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  2784                : MATRIX_MODE_LINE_ROW (w->current_matrix)));
  2785       gy = gr->y;
  2786       area = TEXT_AREA;
  2787       goto text_glyph_row_found;
  2788 
  2789     case ON_TEXT:
  2790       area = TEXT_AREA;
  2791 
  2792     text_glyph:
  2793       gr = 0; gy = 0;
  2794       for (; r <= end_row && r->enabled_p; ++r)
  2795         if (r->y + r->height > y)
  2796           {
  2797             gr = r; gy = r->y;
  2798             break;
  2799           }
  2800 
  2801     text_glyph_row_found:
  2802       if (gr && gy <= y)
  2803         {
  2804           struct glyph *g = gr->glyphs[area];
  2805           struct glyph *end = g + gr->used[area];
  2806 
  2807           height = gr->height;
  2808           for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
  2809             if (gx + g->pixel_width > x)
  2810               break;
  2811 
  2812           if (g < end)
  2813             {
  2814               if (g->type == IMAGE_GLYPH)
  2815                 {
  2816                   /* Don't remember when mouse is over image, as
  2817                      image may have hot-spots.  */
  2818                   STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
  2819                   return;
  2820                 }
  2821               width = g->pixel_width;
  2822             }
  2823           else
  2824             {
  2825               /* Use nominal char spacing at end of line.  */
  2826               x -= gx;
  2827               gx += (x / width) * width;
  2828             }
  2829 
  2830           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2831               && part != ON_TAB_LINE)
  2832             {
  2833               gx += window_box_left_offset (w, area);
  2834               /* Don't expand over the modeline to make sure the vertical
  2835                  drag cursor is shown early enough.  */
  2836               height = min (height,
  2837                             max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2838             }
  2839         }
  2840       else
  2841         {
  2842           /* Use nominal line height at end of window.  */
  2843           gx = (x / width) * width;
  2844           y -= gy;
  2845           gy += (y / height) * height;
  2846           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2847               && part != ON_TAB_LINE)
  2848             /* See comment above.  */
  2849             height = min (height,
  2850                           max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2851         }
  2852       break;
  2853 
  2854     case ON_LEFT_FRINGE:
  2855       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2856             ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
  2857             : window_box_right_offset (w, LEFT_MARGIN_AREA));
  2858       width = WINDOW_LEFT_FRINGE_WIDTH (w);
  2859       goto row_glyph;
  2860 
  2861     case ON_RIGHT_FRINGE:
  2862       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2863             ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2864             : window_box_right_offset (w, TEXT_AREA));
  2865       if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
  2866           && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
  2867           && !WINDOW_RIGHTMOST_P (w))
  2868         if (gx < WINDOW_PIXEL_WIDTH (w) - width)
  2869           /* Make sure the vertical border can get her own glyph to the
  2870              right of the one we build here.  */
  2871           width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
  2872         else
  2873           width = WINDOW_PIXEL_WIDTH (w) - gx;
  2874       else
  2875         width = WINDOW_RIGHT_FRINGE_WIDTH (w);
  2876 
  2877       goto row_glyph;
  2878 
  2879     case ON_VERTICAL_BORDER:
  2880       gx = WINDOW_PIXEL_WIDTH (w) - width;
  2881       goto row_glyph;
  2882 
  2883     case ON_VERTICAL_SCROLL_BAR:
  2884       gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
  2885             ? 0
  2886             : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2887                + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2888                   ? WINDOW_RIGHT_FRINGE_WIDTH (w)
  2889                   : 0)));
  2890       width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  2891 
  2892     row_glyph:
  2893       gr = 0, gy = 0;
  2894       for (; r <= end_row && r->enabled_p; ++r)
  2895         if (r->y + r->height > y)
  2896           {
  2897             gr = r; gy = r->y;
  2898             break;
  2899           }
  2900 
  2901       if (gr && gy <= y)
  2902         height = gr->height;
  2903       else
  2904         {
  2905           /* Use nominal line height at end of window.  */
  2906           y -= gy;
  2907           gy += (y / height) * height;
  2908         }
  2909       break;
  2910 
  2911     case ON_RIGHT_DIVIDER:
  2912       gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2913       width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2914       gy = 0;
  2915       /* The bottom divider prevails. */
  2916       height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2917       goto add_edge;
  2918 
  2919     case ON_BOTTOM_DIVIDER:
  2920       gx = 0;
  2921       width = WINDOW_PIXEL_WIDTH (w);
  2922       gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2923       height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2924       goto add_edge;
  2925 
  2926     default:
  2927       ;
  2928     virtual_glyph:
  2929       /* If there is no glyph under the mouse, then we divide the screen
  2930          into a grid of the smallest glyph in the frame, and use that
  2931          as our "glyph".  */
  2932 
  2933       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
  2934          round down even for negative values.  */
  2935       if (gx < 0)
  2936         gx -= width - 1;
  2937       if (gy < 0)
  2938         gy -= height - 1;
  2939 
  2940       gx = (gx / width) * width;
  2941       gy = (gy / height) * height;
  2942 
  2943       goto store_rect;
  2944     }
  2945 
  2946  add_edge:
  2947   gx += WINDOW_LEFT_EDGE_X (w);
  2948   gy += WINDOW_TOP_EDGE_Y (w);
  2949 
  2950  store_rect:
  2951   STORE_NATIVE_RECT (*rect, gx, gy, width, height);
  2952 
  2953   /* Visible feedback for debugging.  */
  2954 #if false && defined HAVE_X_WINDOWS
  2955   XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
  2956                   f->output_data.x->normal_gc,
  2957                   gx, gy, width, height);
  2958 #endif
  2959 }
  2960 
  2961 
  2962 #endif /* HAVE_WINDOW_SYSTEM */
  2963 
  2964 static void
  2965 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
  2966 {
  2967   eassert (w);
  2968   w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
  2969   w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
  2970   w->window_end_vpos
  2971     = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
  2972 }
  2973 
  2974 static bool
  2975 hscrolling_current_line_p (struct window *w)
  2976 {
  2977   return (!w->suspend_auto_hscroll
  2978           && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
  2979                  Qcurrent_line));
  2980 }
  2981 
  2982 /***********************************************************************
  2983                         Lisp form evaluation
  2984  ***********************************************************************/
  2985 
  2986 /* Error handler for safe_eval and safe_call.  */
  2987 
  2988 static Lisp_Object
  2989 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
  2990 {
  2991   add_to_log ("Error during redisplay: %S signaled %S",
  2992               Flist (nargs, args), arg);
  2993   return Qnil;
  2994 }
  2995 
  2996 /* Call function FUNC with the rest of NARGS - 1 arguments
  2997    following.  Return the result, or nil if something went
  2998    wrong.  Prevent redisplay during the evaluation.  */
  2999 
  3000 static Lisp_Object
  3001 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
  3002 {
  3003   Lisp_Object val;
  3004 
  3005   if (inhibit_eval_during_redisplay)
  3006     val = Qnil;
  3007   else
  3008     {
  3009       ptrdiff_t i;
  3010       specpdl_ref count = SPECPDL_INDEX ();
  3011       Lisp_Object *args;
  3012       USE_SAFE_ALLOCA;
  3013       SAFE_ALLOCA_LISP (args, nargs);
  3014 
  3015       args[0] = func;
  3016       for (i = 1; i < nargs; i++)
  3017         args[i] = va_arg (ap, Lisp_Object);
  3018 
  3019       specbind (Qinhibit_redisplay, Qt);
  3020       if (inhibit_quit)
  3021         specbind (Qinhibit_quit, Qt);
  3022       /* Use Qt to ensure debugger does not run,
  3023          so there is no possibility of wanting to redisplay.  */
  3024       val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
  3025                                        safe_eval_handler);
  3026       val = SAFE_FREE_UNBIND_TO (count, val);
  3027     }
  3028 
  3029   return val;
  3030 }
  3031 
  3032 Lisp_Object
  3033 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
  3034 {
  3035   Lisp_Object retval;
  3036   va_list ap;
  3037 
  3038   va_start (ap, func);
  3039   retval = safe__call (false, nargs, func, ap);
  3040   va_end (ap);
  3041   return retval;
  3042 }
  3043 
  3044 /* Call function FN with one argument ARG.
  3045    Return the result, or nil if something went wrong.  */
  3046 
  3047 Lisp_Object
  3048 safe_call1 (Lisp_Object fn, Lisp_Object arg)
  3049 {
  3050   return safe_call (2, fn, arg);
  3051 }
  3052 
  3053 static Lisp_Object
  3054 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
  3055 {
  3056   Lisp_Object retval;
  3057   va_list ap;
  3058 
  3059   va_start (ap, fn);
  3060   retval = safe__call (inhibit_quit, 2, fn, ap);
  3061   va_end (ap);
  3062   return retval;
  3063 }
  3064 
  3065 Lisp_Object
  3066 safe_eval (Lisp_Object sexpr)
  3067 {
  3068   return safe__call1 (false, Qeval, sexpr);
  3069 }
  3070 
  3071 static Lisp_Object
  3072 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
  3073 {
  3074   return safe__call1 (inhibit_quit, Qeval, sexpr);
  3075 }
  3076 
  3077 /* Call function FN with two arguments ARG1 and ARG2.
  3078    Return the result, or nil if something went wrong.  */
  3079 
  3080 Lisp_Object
  3081 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
  3082 {
  3083   return safe_call (3, fn, arg1, arg2);
  3084 }
  3085 
  3086 
  3087 
  3088 /***********************************************************************
  3089                               Debugging
  3090  ***********************************************************************/
  3091 
  3092 /* Define CHECK_IT to perform sanity checks on iterators.
  3093    This is for debugging.  It is too slow to do unconditionally.  */
  3094 
  3095 static void
  3096 CHECK_IT (struct it *it)
  3097 {
  3098 #if false
  3099   if (it->method == GET_FROM_STRING)
  3100     {
  3101       eassert (STRINGP (it->string));
  3102       eassert (IT_STRING_CHARPOS (*it) >= 0);
  3103     }
  3104   else
  3105     {
  3106       eassert (IT_STRING_CHARPOS (*it) < 0);
  3107       if (it->method == GET_FROM_BUFFER)
  3108         {
  3109           /* Check that character and byte positions agree.  */
  3110           eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
  3111         }
  3112     }
  3113 
  3114   if (it->dpvec)
  3115     eassert (it->current.dpvec_index >= 0);
  3116   else
  3117     eassert (it->current.dpvec_index < 0);
  3118 #endif
  3119 }
  3120 
  3121 
  3122 /* Check that the window end of window W is what we expect it
  3123    to be---the last row in the current matrix displaying text.  */
  3124 
  3125 static void
  3126 CHECK_WINDOW_END (struct window *w)
  3127 {
  3128 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
  3129   if (!MINI_WINDOW_P (w) && w->window_end_valid)
  3130     {
  3131       struct glyph_row *row;
  3132       eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
  3133                 !row->enabled_p
  3134                 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
  3135                 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
  3136     }
  3137 #endif
  3138 }
  3139 
  3140 /***********************************************************************
  3141                        Iterator initialization
  3142  ***********************************************************************/
  3143 
  3144 /* Initialize IT for displaying current_buffer in window W, starting
  3145    at character position CHARPOS.  CHARPOS < 0 means that no buffer
  3146    position is specified which is useful when the iterator is assigned
  3147    a position later.  BYTEPOS is the byte position corresponding to
  3148    CHARPOS.
  3149 
  3150    If ROW is not null, calls to produce_glyphs with IT as parameter
  3151    will produce glyphs in that row.
  3152 
  3153    BASE_FACE_ID is the id of a base face to use.  It must be one of
  3154    DEFAULT_FACE_ID for normal text, MODE_LINE_ACTIVE_FACE_ID,
  3155    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
  3156    mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
  3157 
  3158    If ROW is null and BASE_FACE_ID is equal to MODE_LINE_ACTIVE_FACE_ID,
  3159    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
  3160    will be initialized to use the corresponding mode line glyph row of
  3161    the desired matrix of W.  */
  3162 
  3163 void
  3164 init_iterator (struct it *it, struct window *w,
  3165                ptrdiff_t charpos, ptrdiff_t bytepos,
  3166                struct glyph_row *row, enum face_id base_face_id)
  3167 {
  3168   enum face_id remapped_base_face_id = base_face_id;
  3169   int body_width = 0, body_height = 0;
  3170 
  3171   /* Some precondition checks.  */
  3172   eassert (w != NULL && it != NULL);
  3173   eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
  3174                            && charpos <= ZV));
  3175 
  3176   /* If face attributes have been changed since the last redisplay,
  3177      free realized faces now because they depend on face definitions
  3178      that might have changed.  Don't free faces while there might be
  3179      desired matrices pending which reference these faces.  */
  3180   if (!inhibit_free_realized_faces)
  3181     {
  3182       if (face_change)
  3183         {
  3184           face_change = false;
  3185           XFRAME (w->frame)->face_change = 0;
  3186           free_all_realized_faces (Qnil);
  3187         }
  3188       else if (XFRAME (w->frame)->face_change)
  3189         {
  3190           XFRAME (w->frame)->face_change = 0;
  3191           free_all_realized_faces (w->frame);
  3192         }
  3193     }
  3194 
  3195   /* Perhaps remap BASE_FACE_ID to a user-specified alternative.  */
  3196   if (! NILP (Vface_remapping_alist))
  3197     remapped_base_face_id
  3198       = lookup_basic_face (w, XFRAME (w->frame), base_face_id);
  3199 
  3200   /* Use one of the mode line rows of W's desired matrix if
  3201      appropriate.  */
  3202   if (row == NULL)
  3203     {
  3204       if (base_face_id == MODE_LINE_ACTIVE_FACE_ID
  3205           || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
  3206         row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
  3207       else if (base_face_id == TAB_LINE_FACE_ID)
  3208         row = MATRIX_TAB_LINE_ROW (w->desired_matrix);
  3209       else if (base_face_id == HEADER_LINE_FACE_ID)
  3210         {
  3211           /* Header line row depends on whether tab line is enabled.  */
  3212           w->desired_matrix->tab_line_p = window_wants_tab_line (w);
  3213           row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
  3214         }
  3215     }
  3216 
  3217   /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
  3218      Other parts of redisplay rely on that.  */
  3219   memclear (it, sizeof *it);
  3220   it->current.overlay_string_index = -1;
  3221   it->current.dpvec_index = -1;
  3222   it->base_face_id = remapped_base_face_id;
  3223   IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  3224   it->paragraph_embedding = L2R;
  3225   it->bidi_it.w = w;
  3226 
  3227   /* The window in which we iterate over current_buffer:  */
  3228   XSETWINDOW (it->window, w);
  3229   it->w = w;
  3230   it->f = XFRAME (w->frame);
  3231 
  3232   it->cmp_it.id = -1;
  3233   it->cmp_it.parent_it = it;
  3234 
  3235   if (max_redisplay_ticks > 0)
  3236     update_redisplay_ticks (0, w);
  3237 
  3238   /* Extra space between lines (on window systems only).  */
  3239   if (base_face_id == DEFAULT_FACE_ID
  3240       && FRAME_WINDOW_P (it->f))
  3241     {
  3242       if (FIXNATP (BVAR (current_buffer, extra_line_spacing)))
  3243         it->extra_line_spacing = XFIXNAT (BVAR (current_buffer, extra_line_spacing));
  3244       else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
  3245         it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
  3246                                   * FRAME_LINE_HEIGHT (it->f));
  3247       else if (it->f->extra_line_spacing > 0)
  3248         it->extra_line_spacing = it->f->extra_line_spacing;
  3249     }
  3250 
  3251   /* If realized faces have been removed, e.g. because of face
  3252      attribute changes of named faces, recompute them.  When running
  3253      in batch mode, the face cache of the initial frame is null.  If
  3254      we happen to get called, make a dummy face cache.  */
  3255   if (FRAME_FACE_CACHE (it->f) == NULL)
  3256     init_frame_faces (it->f);
  3257   if (FRAME_FACE_CACHE (it->f)->used == 0)
  3258     recompute_basic_faces (it->f);
  3259 
  3260   it->override_ascent = -1;
  3261 
  3262   /* Are control characters displayed as `^C'?  */
  3263   it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
  3264 
  3265   /* -1 means everything between a CR and the following line end
  3266      is invisible.  >0 means lines indented more than this value are
  3267      invisible.  */
  3268   it->selective = (FIXNUMP (BVAR (current_buffer, selective_display))
  3269                    ? (clip_to_bounds
  3270                       (-1, XFIXNUM (BVAR (current_buffer, selective_display)),
  3271                        PTRDIFF_MAX))
  3272                    : (!NILP (BVAR (current_buffer, selective_display))
  3273                       ? -1 : 0));
  3274   it->selective_display_ellipsis_p
  3275     = !NILP (BVAR (current_buffer, selective_display_ellipses));
  3276 
  3277   /* Display table to use.  */
  3278   it->dp = window_display_table (w);
  3279 
  3280   /* Are multibyte characters enabled in current_buffer?  */
  3281   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  3282 
  3283   it->tab_width = SANE_TAB_WIDTH (current_buffer);
  3284 
  3285   /* Are lines in the display truncated?  */
  3286   if (TRUNCATE != 0)
  3287     it->line_wrap = TRUNCATE;
  3288   if (base_face_id == DEFAULT_FACE_ID
  3289       && !it->w->hscroll
  3290       && (WINDOW_FULL_WIDTH_P (it->w)
  3291           || NILP (Vtruncate_partial_width_windows)
  3292           || (FIXNUMP (Vtruncate_partial_width_windows)
  3293               /* PXW: Shall we do something about this?  */
  3294               && (XFIXNUM (Vtruncate_partial_width_windows)
  3295                   <= WINDOW_TOTAL_COLS (it->w))))
  3296       && NILP (BVAR (current_buffer, truncate_lines)))
  3297     it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
  3298       ? WINDOW_WRAP : WORD_WRAP;
  3299 
  3300   /* Get dimensions of truncation and continuation glyphs.  These are
  3301      displayed as fringe bitmaps under X, but we need them for such
  3302      frames when the fringes are turned off.  The no_special_glyphs slot
  3303      of the iterator's frame, when set, suppresses their display - by
  3304      default for tooltip frames and when set via the 'no-special-glyphs'
  3305      frame parameter.  */
  3306 #ifdef HAVE_WINDOW_SYSTEM
  3307   if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
  3308 #endif
  3309     {
  3310       if (it->line_wrap == TRUNCATE)
  3311         {
  3312           /* We will need the truncation glyph.  */
  3313           eassert (it->glyph_row == NULL);
  3314           produce_special_glyphs (it, IT_TRUNCATION);
  3315           it->truncation_pixel_width = it->pixel_width;
  3316         }
  3317       else
  3318         {
  3319           /* We will need the continuation glyph.  */
  3320           eassert (it->glyph_row == NULL);
  3321           produce_special_glyphs (it, IT_CONTINUATION);
  3322           it->continuation_pixel_width = it->pixel_width;
  3323         }
  3324     }
  3325 
  3326   /* Reset these values to zero because the produce_special_glyphs
  3327      above has changed them.  */
  3328   it->pixel_width = it->ascent = it->descent = 0;
  3329   it->phys_ascent = it->phys_descent = 0;
  3330 
  3331   /* Set this after getting the dimensions of truncation and
  3332      continuation glyphs, so that we don't produce glyphs when calling
  3333      produce_special_glyphs, above.  */
  3334   it->glyph_row = row;
  3335   it->area = TEXT_AREA;
  3336 
  3337   /* Get the dimensions of the display area.  The display area
  3338      consists of the visible window area plus a horizontally scrolled
  3339      part to the left of the window.  All x-values are relative to the
  3340      start of this total display area.  */
  3341   if (base_face_id != DEFAULT_FACE_ID)
  3342     {
  3343       /* Mode lines, menu bar in terminal frames.  */
  3344       it->first_visible_x = 0;
  3345       it->last_visible_x =
  3346         WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  3347     }
  3348   else
  3349     {
  3350       /* When hscrolling only the current line, don't apply the
  3351          hscroll here, it will be applied by display_line when it gets
  3352          to laying out the line showing point.  However, if the
  3353          window's min_hscroll is positive, the user specified a lower
  3354          bound for automatic hscrolling, so they expect the
  3355          non-current lines to obey that hscroll amount.  */
  3356       if (hscrolling_current_line_p (w))
  3357         {
  3358           if (w->min_hscroll > 0)
  3359             it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
  3360           else
  3361             it->first_visible_x = 0;
  3362         }
  3363       else
  3364         it->first_visible_x =
  3365           window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
  3366 
  3367       body_width = window_box_width (w, TEXT_AREA);
  3368       if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3369           && body_width != w->old_body_pixel_width)
  3370         FRAME_WINDOW_CHANGE (it->f) = true;
  3371       it->last_visible_x = it->first_visible_x + body_width;
  3372 
  3373       /* If we truncate lines, leave room for the truncation glyph(s) at
  3374          the right margin.  Otherwise, leave room for the continuation
  3375          glyph(s).  Done only if the window has no right fringe.  */
  3376       if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
  3377         {
  3378           if (it->line_wrap == TRUNCATE)
  3379             it->last_visible_x -= it->truncation_pixel_width;
  3380           else
  3381             it->last_visible_x -= it->continuation_pixel_width;
  3382         }
  3383 
  3384       it->tab_line_p = window_wants_tab_line (w);
  3385       it->header_line_p = window_wants_header_line (w);
  3386       body_height = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  3387       it->current_y =  body_height + w->vscroll;
  3388     }
  3389 
  3390   /* Leave room for a border glyph.  */
  3391   if (!FRAME_WINDOW_P (it->f)
  3392       && !WINDOW_RIGHTMOST_P (it->w))
  3393     it->last_visible_x -= 1;
  3394 
  3395   it->last_visible_y = window_text_bottom_y (w);
  3396   body_height += it->last_visible_y;
  3397   if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3398       && body_height != w->old_body_pixel_height)
  3399     FRAME_WINDOW_CHANGE (it->f) = true;
  3400 
  3401   /* For mode lines and alike, arrange for the first glyph having a
  3402      left box line if the face specifies a box.  */
  3403   if (base_face_id != DEFAULT_FACE_ID)
  3404     {
  3405       struct face *face;
  3406 
  3407       it->face_id = remapped_base_face_id;
  3408 
  3409       /* If we have a boxed mode line, make the first character appear
  3410          with a left box line.  */
  3411       face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
  3412       if (face && face->box != FACE_NO_BOX)
  3413         {
  3414           int box_thickness = face->box_vertical_line_width;
  3415           it->face_box_p = true;
  3416           it->start_of_box_run_p = true;
  3417           /* Make sure we will have enough horizontal space to add the
  3418              right box line at the end.  */
  3419           if (box_thickness > 0)
  3420             it->last_visible_x -= box_thickness;
  3421         }
  3422     }
  3423 
  3424   /* If a buffer position was specified, set the iterator there,
  3425      getting overlays and face properties from that position.  */
  3426   if (charpos >= BUF_BEG (current_buffer))
  3427     {
  3428       it->stop_charpos = charpos;
  3429       it->end_charpos = ZV;
  3430       eassert (charpos == BYTE_TO_CHAR (bytepos));
  3431       IT_CHARPOS (*it) = charpos;
  3432       IT_BYTEPOS (*it) = bytepos;
  3433 
  3434       /* We will rely on `reseat' to set this up properly, via
  3435          handle_face_prop.  */
  3436       it->face_id = it->base_face_id;
  3437 
  3438       it->start = it->current;
  3439       /* Do we need to reorder bidirectional text?  Not if this is a
  3440          unibyte buffer: by definition, none of the single-byte
  3441          characters are strong R2L, so no reordering is needed.  And
  3442          bidi.c doesn't support unibyte buffers anyway.  Also, don't
  3443          reorder while we are loading loadup.el, since the tables of
  3444          character properties needed for reordering are not yet
  3445          available.  */
  3446       it->bidi_p =
  3447         !redisplay__inhibit_bidi
  3448         && !NILP (BVAR (current_buffer, bidi_display_reordering))
  3449         && it->multibyte_p;
  3450 
  3451       /* If we are to reorder bidirectional text, init the bidi
  3452          iterator.  */
  3453       if (it->bidi_p)
  3454         {
  3455           /*  Since we don't know at this point whether there will be
  3456               any R2L lines in the window, we reserve space for
  3457               truncation/continuation glyphs even if only the left
  3458               fringe is absent.  */
  3459           if (base_face_id == DEFAULT_FACE_ID
  3460               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
  3461               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
  3462             {
  3463               if (it->line_wrap == TRUNCATE)
  3464                 it->last_visible_x -= it->truncation_pixel_width;
  3465               else
  3466                 it->last_visible_x -= it->continuation_pixel_width;
  3467             }
  3468           /* Note the paragraph direction that this buffer wants to
  3469              use.  */
  3470           if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3471                   Qleft_to_right))
  3472             it->paragraph_embedding = L2R;
  3473           else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3474                        Qright_to_left))
  3475             it->paragraph_embedding = R2L;
  3476           else
  3477             it->paragraph_embedding = NEUTRAL_DIR;
  3478           bidi_unshelve_cache (NULL, false);
  3479           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  3480                         &it->bidi_it);
  3481         }
  3482 
  3483       /* This is set only when long_line_optimizations_p is non-zero
  3484          for the current buffer.  */
  3485       it->medium_narrowing_begv = 0;
  3486 
  3487       /* Compute faces etc.  */
  3488       reseat (it, it->current.pos, true);
  3489     }
  3490 
  3491   CHECK_IT (it);
  3492 }
  3493 
  3494 /* How Emacs deals with long lines.
  3495 
  3496    (1) When a buffer is about to be (re)displayed, 'redisplay_window'
  3497    detects, with a heuristic, whether it contains long lines.
  3498 
  3499    This happens in 'redisplay_window' because it is only displaying
  3500    buffers with long lines that is problematic.  In other words, none
  3501    of the optimizations described below is ever used in buffers that
  3502    are never displayed.
  3503 
  3504    This happens with a heuristic, which checks whether a buffer
  3505    contains long lines, each time its contents have changed "enough"
  3506    between two redisplay cycles, because a buffer without long lines
  3507    can become a buffer with long lines at any time, for example after
  3508    a yank command, or after a replace command, or while the output of
  3509    an external process is inserted in a buffer.
  3510 
  3511    When Emacs has detected that a buffer contains long lines, the
  3512    buffer-local variable 'long_line_optimizations_p' (in 'struct
  3513    buffer') is set, and Emacs does not try to detect whether the
  3514    buffer does or does not contain long lines anymore.
  3515 
  3516    What a long line is depends on the variable 'long-line-threshold',
  3517    whose default value is 50000 (characters).
  3518 
  3519    (2) When a buffer with long lines is (re)displayed, the amount of
  3520    data that the display routines consider is, in a few well-chosen
  3521    places, limited with a temporary restriction, whose bounds are
  3522    calculated with the functions below.
  3523 
  3524    (2.1) 'get_small_narrowing_begv' is used to create a restriction
  3525    which starts a few hundred characters before point.  The exact
  3526    number of characters depends on the width of the window in which
  3527    the buffer is displayed.
  3528 
  3529    There is no corresponding 'get_small_narrowing_zv' function,
  3530    because it is not necessary to set the end limit of that
  3531    restriction.
  3532 
  3533    This restriction is used in four places, namely:
  3534    'back_to_previous_line_start' and 'move_it_vertically_backward'
  3535    (with the 'SET_WITH_NARROWED_BEGV' macro), and in
  3536    'composition_compute_stop_pos' and 'find_automatic_composition' (in
  3537    a conditional statement depending on 'long_line_optimizations_p').
  3538 
  3539    (2.2) 'get_medium_narrowing_begv' is used to create a restriction
  3540    which starts a few thousand characters before point.  The exact
  3541    number of characters depends on the size (width and height) of the
  3542    window in which the buffer is displayed.  For performance reasons,
  3543    the return value of that function is cached in 'struct it', in the
  3544    'medium_narrowing_begv' field.
  3545 
  3546    The corresponding function 'get_medium_narrowing_zv' (and
  3547    'medium_narrowing_zv' field in 'struct it') is not used to set the
  3548    end limit of the restriction, which is again unnecessary, but to
  3549    determine, in 'reseat', whether the iterator has moved far enough
  3550    from its original position, and whether the start position of the
  3551    restriction must be computed anew.
  3552 
  3553    This restriction is used in a single place:
  3554    'get_visually_first_element', with the 'SET_WITH_NARROWED_BEGV'
  3555    macro.
  3556 
  3557    (2.3) 'get_large_narrowing_begv' and 'get_large_narrowing_zv' are
  3558    used to create a restriction which starts a few hundred thousand
  3559    characters before point and ends a few hundred thousand characters
  3560    after point.  The size of that restriction depends on the variable
  3561    'long-line-optimizations-region-size', whose default value is
  3562    500000 (characters); it can be adjusted by a few hundred characters
  3563    depending on 'long-line-optimizations-bol-search-limit', whose
  3564    default value is 128 (characters).
  3565 
  3566    For performance reasons again, the return values of these functions
  3567    are stored in the 'large_narrowing_begv' and 'large_narrowing_zv'
  3568    fields in 'struct it'.
  3569 
  3570    The restriction defined by these values is used around three
  3571    low-level hooks: around 'fontification-functions', in
  3572    'handle_fontified_prop', and around 'pre-command-hook' and
  3573    'post-command-hook', in 'safe_run_hooks_maybe_narrowed', which is
  3574    called in 'command_loop_1'.  These restrictions are set around
  3575    these hooks with 'labeled_narrow_to_region'; the restrictions are
  3576    labeled, and cannot be removed with a call to 'widen', but can be
  3577    removed with 'without-restriction' with a :label argument.
  3578 */
  3579 
  3580 static int
  3581 get_narrowed_width (struct window *w)
  3582 {
  3583   /* In a character-only terminal, only one font size is used, so we
  3584      can use a smaller factor.  */
  3585   int fact = FRAME_WINDOW_P (XFRAME (w->frame)) ? 3 : 2;
  3586   /* If the window has no fringes (in a character-only terminal or in
  3587      a GUI frame without fringes), subtract 1 from the width for the
  3588      '\' line wrapping character.  */
  3589   int width = window_body_width (w, WINDOW_BODY_IN_CANONICAL_CHARS)
  3590     - ((WINDOW_RIGHT_FRINGE_WIDTH (w) == 0
  3591         || WINDOW_LEFT_FRINGE_WIDTH (w) == 0) ? 1 : 0);
  3592   return fact * max (1, width);
  3593 }
  3594 
  3595 static int
  3596 get_narrowed_len (struct window *w)
  3597 {
  3598   int height = window_body_height (w, WINDOW_BODY_IN_CANONICAL_CHARS);
  3599   return get_narrowed_width (w) * max (1, height);
  3600 }
  3601 
  3602 static ptrdiff_t
  3603 get_medium_narrowing_begv (struct window *w, ptrdiff_t pos)
  3604 {
  3605   int len = get_narrowed_len (w);
  3606   return max ((pos / len - 1) * len, BEGV);
  3607 }
  3608 
  3609 static ptrdiff_t
  3610 get_medium_narrowing_zv (struct window *w, ptrdiff_t pos)
  3611 {
  3612   int len = get_narrowed_len (w);
  3613   return min ((pos / len + 1) * len, ZV);
  3614 }
  3615 
  3616 /* Find the position of the last BOL before POS, unless it is too far
  3617    away.  The buffer portion in which the search occurs is gradually
  3618    enlarged: [POS-500..POS], [POS-5500..POS-500],
  3619    [POS-55500..POS-5500], and finally [POS-555500..POS-55500].  Return
  3620    BEGV-1 if no BOL was found in [POS-555500..POS].  */
  3621 static ptrdiff_t
  3622 get_nearby_bol_pos (ptrdiff_t pos)
  3623 {
  3624   ptrdiff_t start, pos_bytepos, cur, next, found, bol = BEGV - 1, init_pos = pos;
  3625   int dist;
  3626   for (dist = 500; dist <= 500000; dist *= 10)
  3627     {
  3628       pos_bytepos = pos == BEGV ? BEGV_BYTE : CHAR_TO_BYTE (pos);
  3629       start = pos - dist < BEGV ? BEGV : pos - dist;
  3630       for (cur = start; cur < pos; cur = next)
  3631         {
  3632           next = find_newline1 (cur, CHAR_TO_BYTE (cur),
  3633                                 pos, pos_bytepos,
  3634                                 1, &found, NULL, false);
  3635           if (found)
  3636             bol = next;
  3637           else
  3638             break;
  3639         }
  3640       if (bol >= BEGV || start == BEGV)
  3641         break;
  3642       else
  3643         pos = pos - dist < BEGV ? BEGV : pos - dist;
  3644     }
  3645   eassert (bol <= init_pos);
  3646   return bol;
  3647 }
  3648 
  3649 ptrdiff_t
  3650 get_small_narrowing_begv (struct window *w, ptrdiff_t pos)
  3651 {
  3652   int len = get_narrowed_width (w);
  3653   ptrdiff_t bol_pos = max (get_nearby_bol_pos (pos), BEGV);
  3654   return max (bol_pos + ((pos - bol_pos) / len - 1) * len, BEGV);
  3655 }
  3656 
  3657 ptrdiff_t
  3658 get_large_narrowing_begv (ptrdiff_t pos)
  3659 {
  3660   if (long_line_optimizations_region_size <= 0)
  3661     return BEGV;
  3662   int len = long_line_optimizations_region_size / 2;
  3663   int begv = max (pos - len, BEGV);
  3664   int limit = long_line_optimizations_bol_search_limit;
  3665   while (limit > 0)
  3666     {
  3667       if (begv == BEGV || FETCH_BYTE (CHAR_TO_BYTE (begv) - 1) == '\n')
  3668         return begv;
  3669       begv--;
  3670       limit--;
  3671     }
  3672   return begv;
  3673 }
  3674 
  3675 ptrdiff_t
  3676 get_large_narrowing_zv (ptrdiff_t pos)
  3677 {
  3678   if (long_line_optimizations_region_size <= 0)
  3679     return ZV;
  3680   int len = long_line_optimizations_region_size / 2;
  3681   return min (pos + len, ZV);
  3682 }
  3683 
  3684 static void
  3685 unwind_narrowed_begv (Lisp_Object point_min)
  3686 {
  3687   SET_BUF_BEGV (current_buffer, XFIXNUM (point_min));
  3688 }
  3689 
  3690 /* Set DST to EXPR.  When IT indicates that BEGV should temporarily be
  3691    updated to optimize display, evaluate EXPR with BEGV set to BV.  */
  3692 
  3693 #define SET_WITH_NARROWED_BEGV(IT,DST,EXPR,BV)                          \
  3694   do {                                                                  \
  3695     if (IT->medium_narrowing_begv)                                      \
  3696       {                                                                 \
  3697         specpdl_ref count = SPECPDL_INDEX ();                           \
  3698         record_unwind_protect (unwind_narrowed_begv, Fpoint_min ());    \
  3699         SET_BUF_BEGV (current_buffer, BV);                              \
  3700         DST = EXPR;                                                     \
  3701         unbind_to (count, Qnil);                                        \
  3702       }                                                                 \
  3703     else                                                                \
  3704       DST = EXPR;                                                       \
  3705   } while (0)
  3706 
  3707 /* Initialize IT for the display of window W with window start POS.  */
  3708 
  3709 void
  3710 start_display (struct it *it, struct window *w, struct text_pos pos)
  3711 {
  3712   struct glyph_row *row;
  3713   int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
  3714 
  3715   row = w->desired_matrix->rows + first_vpos;
  3716   init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
  3717   it->first_vpos = first_vpos;
  3718 
  3719   /* Don't reseat to previous visible line start if current start
  3720      position is in a string or image.  */
  3721   if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
  3722     {
  3723       int first_y = it->current_y;
  3724 
  3725       /* If window start is not at a line start, skip forward to POS to
  3726          get the correct continuation lines width.  */
  3727       bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
  3728                                   || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
  3729       if (!start_at_line_beg_p)
  3730         {
  3731           int new_x;
  3732 
  3733           reseat_at_previous_visible_line_start (it);
  3734           move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
  3735 
  3736           new_x = it->current_x + it->pixel_width;
  3737 
  3738           /* If lines are continued, this line may end in the middle
  3739              of a multi-glyph character (e.g. a control character
  3740              displayed as \003, or in the middle of an overlay
  3741              string).  In this case move_it_to above will not have
  3742              taken us to the start of the continuation line but to the
  3743              end of the continued line.  */
  3744           if (it->current_x > 0
  3745               && it->line_wrap != TRUNCATE /* Lines are continued.  */
  3746               && (/* And glyph doesn't fit on the line.  */
  3747                   new_x > it->last_visible_x
  3748                   /* Or it fits exactly and we're on a window
  3749                      system frame.  */
  3750                   || (new_x == it->last_visible_x
  3751                       && FRAME_WINDOW_P (it->f)
  3752                       && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  3753                           ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
  3754                           : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
  3755             {
  3756               if ((it->current.dpvec_index >= 0
  3757                    || it->current.overlay_string_index >= 0)
  3758                   /* If we are on a newline from a display vector or
  3759                      overlay string, then we are already at the end of
  3760                      a screen line; no need to go to the next line in
  3761                      that case, as this line is not really continued.
  3762                      (If we do go to the next line, C-e will not DTRT.)  */
  3763                   && it->c != '\n')
  3764                 {
  3765                   set_iterator_to_next (it, true);
  3766                   move_it_in_display_line_to (it, -1, -1, 0);
  3767                 }
  3768 
  3769               it->continuation_lines_width += it->current_x;
  3770             }
  3771           /* If the character at POS is displayed via a display
  3772              vector, move_it_to above stops at the final glyph of
  3773              IT->dpvec.  To make the caller redisplay that character
  3774              again (a.k.a. start at POS), we need to reset the
  3775              dpvec_index to the beginning of IT->dpvec.  */
  3776           else if (it->current.dpvec_index >= 0)
  3777             it->current.dpvec_index = 0;
  3778 
  3779           /* We're starting a new display line, not affected by the
  3780              height of the continued line, so clear the appropriate
  3781              fields in the iterator structure.  */
  3782           it->max_ascent = it->max_descent = 0;
  3783           it->max_phys_ascent = it->max_phys_descent = 0;
  3784 
  3785           it->current_y = first_y;
  3786           it->vpos = 0;
  3787           it->current_x = it->hpos = 0;
  3788         }
  3789     }
  3790 }
  3791 
  3792 
  3793 /* Return true if POS is a position in ellipses displayed for invisible
  3794    text.  W is the window we display, for text property lookup.  */
  3795 
  3796 static bool
  3797 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
  3798 {
  3799   Lisp_Object prop, window;
  3800   bool ellipses_p = false;
  3801   ptrdiff_t charpos = CHARPOS (pos->pos);
  3802 
  3803   /* If POS specifies a position in a display vector, this might
  3804      be for an ellipsis displayed for invisible text.  We won't
  3805      get the iterator set up for delivering that ellipsis unless
  3806      we make sure that it gets aware of the invisible text.  */
  3807   if (pos->dpvec_index >= 0
  3808       && pos->overlay_string_index < 0
  3809       && CHARPOS (pos->string_pos) < 0
  3810       && charpos > BEGV
  3811       && (XSETWINDOW (window, w),
  3812           prop = Fget_char_property (make_fixnum (charpos),
  3813                                      Qinvisible, window),
  3814           TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
  3815     {
  3816       prop = Fget_char_property (make_fixnum (charpos - 1), Qinvisible,
  3817                                  window);
  3818       ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
  3819     }
  3820 
  3821   return ellipses_p;
  3822 }
  3823 
  3824 
  3825 /* Initialize IT for stepping through current_buffer in window W,
  3826    starting at position POS that includes overlay string and display
  3827    vector/ control character translation position information.  Value
  3828    is false if there are overlay strings with newlines at POS.  */
  3829 
  3830 static bool
  3831 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
  3832 {
  3833   ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
  3834   int i;
  3835   bool overlay_strings_with_newlines = false;
  3836 
  3837   /* If POS specifies a position in a display vector, this might
  3838      be for an ellipsis displayed for invisible text.  We won't
  3839      get the iterator set up for delivering that ellipsis unless
  3840      we make sure that it gets aware of the invisible text.  */
  3841   if (in_ellipses_for_invisible_text_p (pos, w))
  3842     {
  3843       --charpos;
  3844       bytepos = 0;
  3845     }
  3846 
  3847   /* Keep in mind: the call to reseat in init_iterator skips invisible
  3848      text, so we might end up at a position different from POS.  This
  3849      is only a problem when POS is a row start after a newline and an
  3850      overlay starts there with an after-string, and the overlay has an
  3851      invisible property.  Since we don't skip invisible text in
  3852      display_line and elsewhere immediately after consuming the
  3853      newline before the row start, such a POS will not be in a string,
  3854      but the call to init_iterator below will move us to the
  3855      after-string.  */
  3856   init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
  3857 
  3858   /* This only scans the current chunk -- it should scan all chunks.
  3859      However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
  3860      to 16 in 22.1 to make this a lesser problem.  */
  3861   for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
  3862     {
  3863       const char *s = SSDATA (it->overlay_strings[i]);
  3864       const char *e = s + SBYTES (it->overlay_strings[i]);
  3865 
  3866       while (s < e && *s != '\n')
  3867         ++s;
  3868 
  3869       if (s < e)
  3870         {
  3871           overlay_strings_with_newlines = true;
  3872           break;
  3873         }
  3874     }
  3875 
  3876   /* If position is within an overlay string, set up IT to the right
  3877      overlay string.  */
  3878   if (pos->overlay_string_index >= 0)
  3879     {
  3880       int relative_index;
  3881 
  3882       /* If the first overlay string happens to have a `display'
  3883          property for an image, the iterator will be set up for that
  3884          image, and we have to undo that setup first before we can
  3885          correct the overlay string index.  */
  3886       if (it->method == GET_FROM_IMAGE)
  3887         pop_it (it);
  3888 
  3889       /* We already have the first chunk of overlay strings in
  3890          IT->overlay_strings.  Load more until the one for
  3891          pos->overlay_string_index is in IT->overlay_strings.  */
  3892       if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
  3893         {
  3894           ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
  3895           it->current.overlay_string_index = 0;
  3896           while (n--)
  3897             {
  3898               load_overlay_strings (it, 0);
  3899               it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
  3900             }
  3901         }
  3902 
  3903       it->current.overlay_string_index = pos->overlay_string_index;
  3904       relative_index = (it->current.overlay_string_index
  3905                         % OVERLAY_STRING_CHUNK_SIZE);
  3906       it->string = it->overlay_strings[relative_index];
  3907       eassert (STRINGP (it->string));
  3908       it->current.string_pos = pos->string_pos;
  3909       it->method = GET_FROM_STRING;
  3910       it->end_charpos = SCHARS (it->string);
  3911       /* Set up the bidi iterator for this overlay string.  */
  3912       if (it->bidi_p)
  3913         {
  3914           it->bidi_it.string.lstring = it->string;
  3915           it->bidi_it.string.s = NULL;
  3916           it->bidi_it.string.schars = SCHARS (it->string);
  3917           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  3918           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  3919           it->bidi_it.string.unibyte = !it->multibyte_p;
  3920           it->bidi_it.w = it->w;
  3921           bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3922                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  3923 
  3924           /* Synchronize the state of the bidi iterator with
  3925              pos->string_pos.  For any string position other than
  3926              zero, this will be done automagically when we resume
  3927              iteration over the string and get_visually_first_element
  3928              is called.  But if string_pos is zero, and the string is
  3929              to be reordered for display, we need to resync manually,
  3930              since it could be that the iteration state recorded in
  3931              pos ended at string_pos of 0 moving backwards in string.  */
  3932           if (CHARPOS (pos->string_pos) == 0)
  3933             {
  3934               get_visually_first_element (it);
  3935               if (IT_STRING_CHARPOS (*it) != 0)
  3936                 do {
  3937                   /* Paranoia.  */
  3938                   eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
  3939                   bidi_move_to_visually_next (&it->bidi_it);
  3940                 } while (it->bidi_it.charpos != 0);
  3941             }
  3942           eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  3943                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
  3944         }
  3945     }
  3946 
  3947   if (CHARPOS (pos->string_pos) >= 0)
  3948     {
  3949       /* Recorded position is not in an overlay string, but in another
  3950          string.  This can only be a string from a `display' property.
  3951          IT should already be filled with that string.  */
  3952       it->current.string_pos = pos->string_pos;
  3953       eassert (STRINGP (it->string));
  3954       if (it->bidi_p)
  3955         bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3956                       FRAME_WINDOW_P (it->f), &it->bidi_it);
  3957     }
  3958 
  3959   /* Restore position in display vector translations, control
  3960      character translations or ellipses.  */
  3961   if (pos->dpvec_index >= 0)
  3962     {
  3963       if (it->dpvec == NULL)
  3964         get_next_display_element (it);
  3965       eassert (it->dpvec && it->current.dpvec_index == 0);
  3966       it->current.dpvec_index = pos->dpvec_index;
  3967     }
  3968 
  3969   CHECK_IT (it);
  3970   return !overlay_strings_with_newlines;
  3971 }
  3972 
  3973 
  3974 /* Initialize IT for stepping through current_buffer in window W
  3975    starting at ROW->start.  */
  3976 
  3977 static void
  3978 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
  3979 {
  3980   init_from_display_pos (it, w, &row->start);
  3981   it->start = row->start;
  3982   it->continuation_lines_width = row->continuation_lines_width;
  3983   CHECK_IT (it);
  3984 }
  3985 
  3986 
  3987 /* Initialize IT for stepping through current_buffer in window W
  3988    starting in the line following ROW, i.e. starting at ROW->end.
  3989    Value is false if there are overlay strings with newlines at ROW's
  3990    end position, or if the following row begins with bidi-reordered
  3991    characters that could be composed.  */
  3992 
  3993 static bool
  3994 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
  3995 {
  3996   bool success = false;
  3997 
  3998   if (init_from_display_pos (it, w, &row->end))
  3999     {
  4000       if (row->continued_p)
  4001         it->continuation_lines_width
  4002           = row->continuation_lines_width + row->pixel_width;
  4003       CHECK_IT (it);
  4004       /* Initializing IT in the presence of compositions in reordered
  4005          rows is tricky: row->end above will generally cause us to
  4006          start at position that is not the first one in the logical
  4007          order, and we might therefore miss the composition earlier in
  4008          the buffer that affects how glypsh are laid out in this row.
  4009          So we punt instead.  Note: the test below works because
  4010          get_next_display_element calls get_visually_first_element,
  4011          which calls composition_compute_stop_pos, which populates
  4012          it->cmp_it.  */
  4013       if (get_next_display_element (it)
  4014           && (it->bidi_it.scan_dir == -1 && it->cmp_it.id >= 0))
  4015         success = false;
  4016       else
  4017         success = true;
  4018     }
  4019 
  4020   return success;
  4021 }
  4022 
  4023 
  4024 
  4025 
  4026 /***********************************************************************
  4027                            Text properties
  4028  ***********************************************************************/
  4029 
  4030 /* Called when IT reaches IT->stop_charpos.  Handle text property and
  4031    overlay changes.  Set IT->stop_charpos to the next position where
  4032    to stop.  */
  4033 
  4034 static void
  4035 handle_stop (struct it *it)
  4036 {
  4037   enum prop_handled handled;
  4038   bool handle_overlay_change_p;
  4039   struct props *p;
  4040 
  4041   it->dpvec = NULL;
  4042   it->current.dpvec_index = -1;
  4043   handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
  4044   it->ellipsis_p = false;
  4045 
  4046   /* Use face of preceding text for ellipsis (if invisible) */
  4047   if (it->selective_display_ellipsis_p)
  4048     it->saved_face_id = it->face_id;
  4049 
  4050   /* Here's the description of the semantics of, and the logic behind,
  4051      the various HANDLED_* statuses:
  4052 
  4053      HANDLED_NORMALLY means the handler did its job, and the loop
  4054      should proceed to calling the next handler in order.
  4055 
  4056      HANDLED_RECOMPUTE_PROPS means the handler caused a significant
  4057      change in the properties and overlays at current position, so the
  4058      loop should be restarted, to re-invoke the handlers that were
  4059      already called.  This happens when fontification-functions were
  4060      called by handle_fontified_prop, and actually fontified
  4061      something.  Another case where HANDLED_RECOMPUTE_PROPS is
  4062      returned is when we discover overlay strings that need to be
  4063      displayed right away.  The loop below will continue for as long
  4064      as the status is HANDLED_RECOMPUTE_PROPS.
  4065 
  4066      HANDLED_RETURN means return immediately to the caller, to
  4067      continue iteration without calling any further handlers.  This is
  4068      used when we need to act on some property right away, for example
  4069      when we need to display the ellipsis or a replacing display
  4070      property, such as display string or image.
  4071 
  4072      HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
  4073      consumed, and the handler switched to the next overlay string.
  4074      This signals the loop below to refrain from looking for more
  4075      overlays before all the overlay strings of the current overlay
  4076      are processed.
  4077 
  4078      Some of the handlers called by the loop push the iterator state
  4079      onto the stack (see 'push_it'), and arrange for the iteration to
  4080      continue with another object, such as an image, a display string,
  4081      or an overlay string.  In most such cases, it->stop_charpos is
  4082      set to the first character of the string, so that when the
  4083      iteration resumes, this function will immediately be called
  4084      again, to examine the properties at the beginning of the string.
  4085 
  4086      When a display or overlay string is exhausted, the iterator state
  4087      is popped (see 'pop_it'), and iteration continues with the
  4088      previous object.  Again, in many such cases this function is
  4089      called again to find the next position where properties might
  4090      change.  */
  4091 
  4092   do
  4093     {
  4094       handled = HANDLED_NORMALLY;
  4095 
  4096       /* Call text property handlers.  */
  4097       for (p = it_props; p->handler; ++p)
  4098         {
  4099           handled = p->handler (it);
  4100 
  4101           if (handled == HANDLED_RECOMPUTE_PROPS)
  4102             break;
  4103           else if (handled == HANDLED_RETURN)
  4104             {
  4105               /* We still want to show before and after strings from
  4106                  overlays even if the actual buffer text is replaced.  */
  4107               if (!handle_overlay_change_p
  4108                   || it->sp > 1
  4109                   /* Don't call get_overlay_strings_1 if we already
  4110                      have overlay strings loaded, because doing so
  4111                      will load them again and push the iterator state
  4112                      onto the stack one more time, which is not
  4113                      expected by the rest of the code that processes
  4114                      overlay strings.  */
  4115                   || (it->current.overlay_string_index < 0
  4116                       && !get_overlay_strings_1 (it, 0, false)))
  4117                 {
  4118                   if (it->ellipsis_p)
  4119                     setup_for_ellipsis (it, 0);
  4120                   /* When handling a display spec, we might load an
  4121                      empty string.  In that case, discard it here.  We
  4122                      used to discard it in handle_single_display_spec,
  4123                      but that causes get_overlay_strings_1, above, to
  4124                      ignore overlay strings that we must check.  */
  4125                   if (STRINGP (it->string) && !SCHARS (it->string))
  4126                     pop_it (it);
  4127                   return;
  4128                 }
  4129               else if (STRINGP (it->string) && !SCHARS (it->string))
  4130                 pop_it (it);
  4131               else
  4132                 {
  4133                   it->string_from_display_prop_p = false;
  4134                   it->from_disp_prop_p = false;
  4135                   handle_overlay_change_p = false;
  4136                 }
  4137               handled = HANDLED_RECOMPUTE_PROPS;
  4138               break;
  4139             }
  4140           else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
  4141             handle_overlay_change_p = false;
  4142         }
  4143 
  4144       if (handled != HANDLED_RECOMPUTE_PROPS)
  4145         {
  4146           /* Don't check for overlay strings below when set to deliver
  4147              characters from a display vector.  */
  4148           if (it->method == GET_FROM_DISPLAY_VECTOR)
  4149             handle_overlay_change_p = false;
  4150 
  4151           /* Handle overlay changes.
  4152              This sets HANDLED to HANDLED_RECOMPUTE_PROPS
  4153              if it finds overlays.  */
  4154           if (handle_overlay_change_p)
  4155             handled = handle_overlay_change (it);
  4156         }
  4157 
  4158       if (it->ellipsis_p)
  4159         {
  4160           setup_for_ellipsis (it, 0);
  4161           break;
  4162         }
  4163     }
  4164   while (handled == HANDLED_RECOMPUTE_PROPS);
  4165 
  4166   /* Determine where to stop next.  */
  4167   if (handled == HANDLED_NORMALLY)
  4168     compute_stop_pos (it);
  4169 }
  4170 
  4171 
  4172 /* Compute IT->stop_charpos from text property and overlay change
  4173    information for IT's current position.  */
  4174 
  4175 static void
  4176 compute_stop_pos (struct it *it)
  4177 {
  4178   register INTERVAL iv, next_iv;
  4179   Lisp_Object object, limit, position;
  4180   ptrdiff_t charpos, bytepos;
  4181 
  4182   if (STRINGP (it->string))
  4183     {
  4184       /* Strings are usually short, so don't limit the search for
  4185          properties.  */
  4186       it->stop_charpos = it->end_charpos;
  4187       object = it->string;
  4188       limit = Qnil;
  4189       charpos = IT_STRING_CHARPOS (*it);
  4190       bytepos = IT_STRING_BYTEPOS (*it);
  4191     }
  4192   else
  4193     {
  4194       ptrdiff_t pos;
  4195 
  4196       /* If end_charpos is out of range for some reason, such as a
  4197          misbehaving display function, rationalize it (Bug#5984).  */
  4198       if (it->end_charpos > ZV)
  4199         it->end_charpos = ZV;
  4200       it->stop_charpos = it->end_charpos;
  4201 
  4202       /* If next overlay change is in front of the current stop pos
  4203          (which is IT->end_charpos), stop there.  Note: value of
  4204          next_overlay_change is point-max if no overlay change
  4205          follows.  */
  4206       charpos = IT_CHARPOS (*it);
  4207       bytepos = IT_BYTEPOS (*it);
  4208       pos = next_overlay_change (charpos);
  4209       if (pos < it->stop_charpos)
  4210         it->stop_charpos = pos;
  4211       /* If we are breaking compositions at point, stop at point.  */
  4212       if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
  4213           && !NILP (Vauto_composition_mode)
  4214           && composition_break_at_point
  4215           && charpos < PT && PT < it->stop_charpos)
  4216         it->stop_charpos = PT;
  4217 
  4218       /* Set up variables for computing the stop position from text
  4219          property changes.  */
  4220       XSETBUFFER (object, current_buffer);
  4221       pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4222       /* Make sure the above arbitrary limit position is not in the
  4223          middle of composable text, so we don't break compositions by
  4224          submitting the composable text to the shaper in separate
  4225          chunks.  We play safe here by assuming that only SPC, TAB,
  4226          FF, and NL cannot be in some composition; in particular, most
  4227          ASCII punctuation characters could be composed into ligatures.  */
  4228       if (!composition_break_at_point
  4229           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  4230           && !NILP (Vauto_composition_mode))
  4231         {
  4232           ptrdiff_t endpos = charpos + 10 * TEXT_PROP_DISTANCE_LIMIT;
  4233           bool found = false;
  4234 
  4235           if (pos > ZV)
  4236             pos = ZV;
  4237           if (endpos > ZV)
  4238             endpos = ZV;
  4239           ptrdiff_t bpos = CHAR_TO_BYTE (pos);
  4240           while (pos < endpos)
  4241             {
  4242               int ch = fetch_char_advance_no_check (&pos, &bpos);
  4243               if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\f')
  4244                 {
  4245                   found = true;
  4246                   break;
  4247                 }
  4248             }
  4249           if (found)
  4250             pos--;
  4251           else if (it->stop_charpos < endpos)
  4252             pos = it->stop_charpos;
  4253           else
  4254             {
  4255               /* Give up and use the original arbitrary limit.  */
  4256               pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4257             }
  4258         }
  4259       limit = make_fixnum (pos);
  4260     }
  4261 
  4262   /* Get the interval containing IT's position.  Value is a null
  4263      interval if there isn't such an interval.  */
  4264   position = make_fixnum (charpos);
  4265   iv = validate_interval_range (object, &position, &position, false);
  4266   if (iv)
  4267     {
  4268       Lisp_Object values_here[LAST_PROP_IDX];
  4269       struct props *p;
  4270 
  4271       /* Get properties here.  */
  4272       for (p = it_props; p->handler; ++p)
  4273         values_here[p->idx] = textget (iv->plist,
  4274                                        builtin_lisp_symbol (p->name));
  4275 
  4276       /* Look for an interval following iv that has different
  4277          properties.  */
  4278       for (next_iv = next_interval (iv);
  4279            (next_iv
  4280             && (NILP (limit)
  4281                 || XFIXNAT (limit) > next_iv->position));
  4282            next_iv = next_interval (next_iv))
  4283         {
  4284           for (p = it_props; p->handler; ++p)
  4285             {
  4286               Lisp_Object new_value = textget (next_iv->plist,
  4287                                                builtin_lisp_symbol (p->name));
  4288               if (!EQ (values_here[p->idx], new_value))
  4289                 break;
  4290             }
  4291 
  4292           if (p->handler)
  4293             break;
  4294         }
  4295 
  4296       if (next_iv)
  4297         {
  4298           if (FIXNUMP (limit)
  4299               && next_iv->position >= XFIXNAT (limit))
  4300             /* No text property change up to limit.  */
  4301             it->stop_charpos = min (XFIXNAT (limit), it->stop_charpos);
  4302           else
  4303             /* Text properties change in next_iv.  */
  4304             it->stop_charpos = min (it->stop_charpos, next_iv->position);
  4305         }
  4306     }
  4307 
  4308   if (it->cmp_it.id < 0)
  4309     {
  4310       ptrdiff_t stoppos = it->end_charpos;
  4311 
  4312       if (it->bidi_p && it->bidi_it.scan_dir < 0)
  4313         stoppos = -1;
  4314       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
  4315                                     stoppos, it->string);
  4316     }
  4317 
  4318   eassert (STRINGP (it->string)
  4319            || (it->stop_charpos >= BEGV
  4320                && it->stop_charpos >= IT_CHARPOS (*it)));
  4321 }
  4322 
  4323 /* How many characters forward to search for a display property or
  4324    display string.  Searching too far forward makes the bidi display
  4325    sluggish, especially in small windows.  */
  4326 #define MAX_DISP_SCAN 250
  4327 
  4328 /* Return the character position of a display string at or after
  4329    position specified by POSITION.  If no display string exists at or
  4330    after POSITION, return ZV.  A display string is either an overlay
  4331    with `display' property whose value is a string, or a `display'
  4332    text property whose value is a string.  STRING is data about the
  4333    string to iterate; if STRING->lstring is nil, we are iterating a
  4334    buffer.  FRAME_WINDOW_P is true when we are displaying a window
  4335    on a GUI frame.  DISP_PROP is set to zero if we searched
  4336    MAX_DISP_SCAN characters forward without finding any display
  4337    strings, non-zero otherwise.  It is set to 2 if the display string
  4338    uses any kind of `(space ...)' spec that will produce a stretch of
  4339    white space in the text area.  */
  4340 ptrdiff_t
  4341 compute_display_string_pos (struct text_pos *position,
  4342                             struct bidi_string_data *string,
  4343                             struct window *w,
  4344                             bool frame_window_p, int *disp_prop)
  4345 {
  4346   /* OBJECT = nil means current buffer.  */
  4347   Lisp_Object object, object1;
  4348   Lisp_Object pos, spec, limpos;
  4349   bool string_p = string && (STRINGP (string->lstring) || string->s);
  4350   ptrdiff_t eob = string_p ? string->schars : ZV;
  4351   ptrdiff_t begb = string_p ? 0 : BEGV;
  4352   ptrdiff_t bufpos, charpos = CHARPOS (*position);
  4353   ptrdiff_t lim =
  4354     (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
  4355   struct text_pos tpos;
  4356   int rv = 0;
  4357 
  4358   if (string && STRINGP (string->lstring))
  4359     object1 = object = string->lstring;
  4360   else if (w && !string_p)
  4361     {
  4362       XSETWINDOW (object, w);
  4363       object1 = Qnil;
  4364     }
  4365   else
  4366     object1 = object = Qnil;
  4367 
  4368   *disp_prop = 1;
  4369 
  4370   if (charpos >= eob
  4371       /* We don't support display properties whose values are strings
  4372          that have display string properties.  */
  4373       || string->from_disp_str
  4374       /* C strings cannot have display properties.  */
  4375       || (string->s && !STRINGP (object)))
  4376     {
  4377       *disp_prop = 0;
  4378       return eob;
  4379     }
  4380 
  4381   /* If the character at CHARPOS is where the display string begins,
  4382      return CHARPOS.  */
  4383   pos = make_fixnum (charpos);
  4384   if (STRINGP (object))
  4385     bufpos = string->bufpos;
  4386   else
  4387     bufpos = charpos;
  4388   tpos = *position;
  4389   if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
  4390       && (charpos <= begb
  4391           || !EQ (Fget_char_property (make_fixnum (charpos - 1), Qdisplay,
  4392                                       object),
  4393                   spec))
  4394       && (rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos, bufpos,
  4395                                     frame_window_p)))
  4396     {
  4397       if (rv == 2)
  4398         *disp_prop = 2;
  4399       return charpos;
  4400     }
  4401 
  4402   /* Look forward for the first character with a `display' property
  4403      that will replace the underlying text when displayed.  */
  4404   limpos = make_fixnum (lim);
  4405   do {
  4406     pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
  4407     CHARPOS (tpos) = XFIXNAT (pos);
  4408     if (CHARPOS (tpos) >= lim)
  4409       {
  4410         *disp_prop = 0;
  4411         break;
  4412       }
  4413     if (STRINGP (object))
  4414       BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
  4415     else
  4416       BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
  4417     spec = Fget_char_property (pos, Qdisplay, object);
  4418     if (!STRINGP (object))
  4419       bufpos = CHARPOS (tpos);
  4420   } while (NILP (spec)
  4421            || !(rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos,
  4422                                           bufpos, frame_window_p)));
  4423   if (rv == 2)
  4424     *disp_prop = 2;
  4425 
  4426   return CHARPOS (tpos);
  4427 }
  4428 
  4429 /* Return the character position of the end of the display string that
  4430    started at CHARPOS.  If there's no display string at CHARPOS,
  4431    return -1.  A display string is either an overlay with `display'
  4432    property whose value is a string or a `display' text property whose
  4433    value is a string.  */
  4434 ptrdiff_t
  4435 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
  4436 {
  4437   /* OBJECT = nil means current buffer.  */
  4438   Lisp_Object object =
  4439     (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
  4440   Lisp_Object pos = make_fixnum (charpos);
  4441   ptrdiff_t eob =
  4442     (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
  4443 
  4444   if (charpos >= eob || (string->s && !STRINGP (object)))
  4445     return eob;
  4446 
  4447   /* It could happen that the display property or overlay was removed
  4448      since we found it in compute_display_string_pos above.  One way
  4449      this can happen is if JIT font-lock was called (through
  4450      handle_fontified_prop), and jit-lock-functions remove text
  4451      properties or overlays from the portion of buffer that includes
  4452      CHARPOS.  Muse mode is known to do that, for example.  In this
  4453      case, we return -1 to the caller, to signal that no display
  4454      string is actually present at CHARPOS.  See bidi_fetch_char for
  4455      how this is handled.
  4456 
  4457      An alternative would be to never look for display properties past
  4458      it->stop_charpos.  But neither compute_display_string_pos nor
  4459      bidi_fetch_char that calls it know or care where the next
  4460      stop_charpos is.  */
  4461   if (NILP (Fget_char_property (pos, Qdisplay, object)))
  4462     return -1;
  4463 
  4464   /* Look forward for the first character where the `display' property
  4465      changes.  */
  4466   pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
  4467 
  4468   return XFIXNAT (pos);
  4469 }
  4470 
  4471 
  4472 
  4473 /***********************************************************************
  4474                             Fontification
  4475  ***********************************************************************/
  4476 
  4477 /* Handle changes in the `fontified' property of the current buffer by
  4478    calling hook functions from Qfontification_functions to fontify
  4479    regions of text.  */
  4480 
  4481 static enum prop_handled
  4482 handle_fontified_prop (struct it *it)
  4483 {
  4484   Lisp_Object prop, pos;
  4485   enum prop_handled handled = HANDLED_NORMALLY;
  4486 
  4487   if (!NILP (Vmemory_full))
  4488     return handled;
  4489 
  4490   /* Get the value of the `fontified' property at IT's current buffer
  4491      position.  (The `fontified' property doesn't have a special
  4492      meaning in strings.)  If the value is nil, call functions from
  4493      Qfontification_functions.  */
  4494   if (!STRINGP (it->string)
  4495       && it->s == NULL
  4496       && !NILP (Vfontification_functions)
  4497       && !(input_was_pending && redisplay_skip_fontification_on_input)
  4498       && !NILP (Vrun_hooks)
  4499       && (pos = make_fixnum (IT_CHARPOS (*it)),
  4500           prop = Fget_char_property (pos, Qfontified, Qnil),
  4501           /* Ignore the special cased nil value always present at EOB since
  4502              no amount of fontifying will be able to change it.  */
  4503           NILP (prop) && IT_CHARPOS (*it) < Z))
  4504     {
  4505       specpdl_ref count = SPECPDL_INDEX ();
  4506       Lisp_Object val;
  4507       struct buffer *obuf = current_buffer;
  4508       ptrdiff_t begv = BEGV, zv = ZV;
  4509       bool old_clip_changed = current_buffer->clip_changed;
  4510       bool saved_inhibit_flag = it->f->inhibit_clear_image_cache;
  4511 
  4512       val = Vfontification_functions;
  4513       specbind (Qfontification_functions, Qnil);
  4514 
  4515       eassert (it->end_charpos == ZV);
  4516 
  4517       if (current_buffer->long_line_optimizations_p
  4518           && long_line_optimizations_region_size > 0)
  4519         {
  4520           ptrdiff_t begv = it->large_narrowing_begv;
  4521           ptrdiff_t zv = it->large_narrowing_zv;
  4522           ptrdiff_t charpos = IT_CHARPOS (*it);
  4523           if (charpos < begv || charpos > zv)
  4524             {
  4525               begv = get_large_narrowing_begv (charpos);
  4526               zv = get_large_narrowing_zv (charpos);
  4527             }
  4528           if (begv != BEG || zv != Z)
  4529             labeled_narrow_to_region (make_fixnum (begv), make_fixnum (zv),
  4530                                       Qlong_line_optimizations_in_fontification_functions);
  4531         }
  4532 
  4533       /* Don't allow Lisp that runs from 'fontification-functions'
  4534          clear our face and image caches behind our back.  */
  4535       it->f->inhibit_clear_image_cache = true;
  4536 
  4537       if (!CONSP (val) || EQ (XCAR (val), Qlambda))
  4538         safe_call1 (val, pos);
  4539       else
  4540         {
  4541           Lisp_Object fns, fn;
  4542 
  4543           fns = Qnil;
  4544 
  4545           for (; CONSP (val); val = XCDR (val))
  4546             {
  4547               fn = XCAR (val);
  4548 
  4549               if (EQ (fn, Qt))
  4550                 {
  4551                   /* A value of t indicates this hook has a local
  4552                      binding; it means to run the global binding too.
  4553                      In a global value, t should not occur.  If it
  4554                      does, we must ignore it to avoid an endless
  4555                      loop.  */
  4556                   for (fns = Fdefault_value (Qfontification_functions);
  4557                        CONSP (fns);
  4558                        fns = XCDR (fns))
  4559                     {
  4560                       fn = XCAR (fns);
  4561                       if (!EQ (fn, Qt))
  4562                         safe_call1 (fn, pos);
  4563                     }
  4564                 }
  4565               else
  4566                 safe_call1 (fn, pos);
  4567             }
  4568         }
  4569 
  4570       it->f->inhibit_clear_image_cache = saved_inhibit_flag;
  4571       unbind_to (count, Qnil);
  4572 
  4573       /* Fontification functions routinely call `save-restriction'.
  4574          Normally, this tags clip_changed, which can confuse redisplay
  4575          (see discussion in Bug#6671).  Since we don't perform any
  4576          special handling of fontification changes in the case where
  4577          `save-restriction' isn't called, there's no point doing so in
  4578          this case either.  So, if the buffer's restrictions are
  4579          actually left unchanged, reset clip_changed.  */
  4580       if (obuf == current_buffer)
  4581         {
  4582           if (begv == BEGV && zv == ZV)
  4583             current_buffer->clip_changed = old_clip_changed;
  4584         }
  4585       /* There isn't much we can reasonably do to protect against
  4586          misbehaving fontification, but here's a fig leaf.  */
  4587       else if (BUFFER_LIVE_P (obuf))
  4588         set_buffer_internal_1 (obuf);
  4589 
  4590       /* The fontification code may have added/removed text.
  4591          It could do even a lot worse, but let's at least protect against
  4592          the most obvious case where only the text past `pos' gets changed',
  4593          as is/was done in grep.el where some escapes sequences are turned
  4594          into face properties (bug#7876).  */
  4595       it->end_charpos = ZV;
  4596 
  4597       /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
  4598          something.  This avoids an endless loop if they failed to
  4599          fontify the text for which reason ever.  */
  4600       if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
  4601         handled = HANDLED_RECOMPUTE_PROPS;
  4602     }
  4603 
  4604   return handled;
  4605 }
  4606 
  4607 
  4608 
  4609 /***********************************************************************
  4610                                 Faces
  4611  ***********************************************************************/
  4612 
  4613 static int
  4614 face_at_pos (const struct it *it, enum lface_attribute_index attr_filter)
  4615 {
  4616   ptrdiff_t next_stop;
  4617 
  4618   if (!STRINGP (it->string))
  4619     {
  4620       return face_at_buffer_position (it->w,
  4621                                       IT_CHARPOS (*it),
  4622                                       &next_stop,
  4623                                       (IT_CHARPOS (*it)
  4624                                        + TEXT_PROP_DISTANCE_LIMIT),
  4625                                       false, it->base_face_id,
  4626                                       attr_filter);
  4627     }
  4628   else
  4629     {
  4630       int base_face_id;
  4631       ptrdiff_t bufpos;
  4632       int i;
  4633       Lisp_Object from_overlay
  4634         = (it->current.overlay_string_index >= 0
  4635            ? it->string_overlays[it->current.overlay_string_index
  4636                                  % OVERLAY_STRING_CHUNK_SIZE]
  4637            : Qnil);
  4638 
  4639       /* See if we got to this string directly or indirectly from
  4640          an overlay property.  That includes the before-string or
  4641          after-string of an overlay, strings in display properties
  4642          provided by an overlay, their text properties, etc.
  4643 
  4644          FROM_OVERLAY is the overlay that brought us here, or nil if none.  */
  4645       if (! NILP (from_overlay))
  4646         for (i = it->sp - 1; i >= 0; i--)
  4647           {
  4648             if (it->stack[i].current.overlay_string_index >= 0)
  4649               from_overlay
  4650                 = it->string_overlays[it->stack[i].current.overlay_string_index
  4651                                       % OVERLAY_STRING_CHUNK_SIZE];
  4652             else if (! NILP (it->stack[i].from_overlay))
  4653               from_overlay = it->stack[i].from_overlay;
  4654 
  4655             if (!NILP (from_overlay))
  4656               break;
  4657           }
  4658 
  4659       if (! NILP (from_overlay))
  4660         {
  4661           bufpos = IT_CHARPOS (*it);
  4662           /* For a string from an overlay, the base face depends
  4663              only on text properties and ignores overlays.  */
  4664           base_face_id
  4665             = face_for_overlay_string (it->w,
  4666                                        IT_CHARPOS (*it),
  4667                                        &next_stop,
  4668                                        (IT_CHARPOS (*it)
  4669                                         + TEXT_PROP_DISTANCE_LIMIT),
  4670                                        false,
  4671                                        from_overlay, attr_filter);
  4672         }
  4673       else
  4674         {
  4675           bufpos = 0;
  4676 
  4677           /* For strings from a `display' property, use the face at
  4678              IT's current buffer position as the base face to merge
  4679              with, so that overlay strings appear in the same face as
  4680              surrounding text, unless they specify their own faces.
  4681              For strings from wrap-prefix and line-prefix properties,
  4682              use the default face, possibly remapped via
  4683              Vface_remapping_alist.  */
  4684           /* Note that the fact that we use the face at _buffer_
  4685              position means that a 'display' property on an overlay
  4686              string will not inherit the face of that overlay string,
  4687              but will instead revert to the face of buffer text
  4688              covered by the overlay.  This is visible, e.g., when the
  4689              overlay specifies a box face, but neither the buffer nor
  4690              the display string do.  This sounds like a design bug,
  4691              but Emacs always did that since v21.1, so changing that
  4692              might be a big deal.  */
  4693           base_face_id = it->string_from_prefix_prop_p
  4694             ? (!NILP (Vface_remapping_alist)
  4695                ? lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
  4696                : DEFAULT_FACE_ID)
  4697             : underlying_face_id (it);
  4698         }
  4699 
  4700       return face_at_string_position (it->w,
  4701                                       it->string,
  4702                                       IT_STRING_CHARPOS (*it),
  4703                                       bufpos,
  4704                                       &next_stop,
  4705                                       base_face_id, false,
  4706                                       attr_filter);
  4707     } /* !STRINGP (it->string) */
  4708 }
  4709 
  4710 
  4711 /* Set up iterator IT from face properties at its current position.
  4712    Called from handle_stop.  */
  4713 static enum prop_handled
  4714 handle_face_prop (struct it *it)
  4715 {
  4716   specpdl_ref count = SPECPDL_INDEX ();
  4717   /* Don't allow the user to quit out of face-merging code, in case
  4718      this is called when redisplaying a non-selected window, with
  4719      point temporarily moved to window-point.  */
  4720   specbind (Qinhibit_quit, Qt);
  4721   const int new_face_id = face_at_pos (it, 0);
  4722   unbind_to (count, Qnil);
  4723 
  4724 
  4725   /* Is this a start of a run of characters with box face?
  4726      Caveat: this can be called for a freshly initialized
  4727      iterator; face_id is -1 in this case.  We know that the new
  4728      face will not change until limit, i.e. if the new face has a
  4729      box, all characters up to limit will have one.  But, as
  4730      usual, we don't know whether limit is really the end.  */
  4731   if (new_face_id != it->face_id)
  4732     {
  4733       struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
  4734       /* If it->face_id is -1, old_face below will be NULL, see
  4735          the definition of FACE_FROM_ID_OR_NULL.  This will happen
  4736          if this is the initial call that gets the face.  */
  4737       struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  4738 
  4739       /* If the value of face_id of the iterator is -1, we have to
  4740          look in front of IT's position and see whether there is a
  4741          face there that's different from new_face_id.  */
  4742       if (!STRINGP (it->string)
  4743           && !old_face
  4744           && IT_CHARPOS (*it) > BEG)
  4745         {
  4746           const int prev_face_id = face_before_it_pos (it);
  4747 
  4748           old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  4749         }
  4750 
  4751       /* If the new face has a box, but the old face does not,
  4752          this is the start of a run of characters with box face,
  4753          i.e. this character has a shadow on the left side.  */
  4754       it->face_id = new_face_id;
  4755       /* Don't reset the start_of_box_run_p flag, only set it if
  4756          needed.  */
  4757       if (!(it->start_of_box_run_p && old_face && old_face->box))
  4758         it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
  4759                                   && (old_face == NULL || !old_face->box));
  4760       it->face_box_p = new_face->box != FACE_NO_BOX;
  4761     }
  4762 
  4763   return HANDLED_NORMALLY;
  4764 }
  4765 
  4766 
  4767 /* Return the ID of the face ``underlying'' IT's current position,
  4768    which is in a string.  If the iterator is associated with a
  4769    buffer, return the face at IT's current buffer position.
  4770    Otherwise, use the iterator's base_face_id.  */
  4771 
  4772 static int
  4773 underlying_face_id (const struct it *it)
  4774 {
  4775   int face_id = it->base_face_id, i;
  4776 
  4777   eassert (STRINGP (it->string));
  4778 
  4779   for (i = it->sp - 1; i >= 0; --i)
  4780     if (NILP (it->stack[i].string))
  4781       face_id = it->stack[i].face_id;
  4782 
  4783   return face_id;
  4784 }
  4785 
  4786 
  4787 /* Compute the face one character before or after the current position
  4788    of IT, in the visual order.  BEFORE_P means get the face
  4789    in front (to the left in L2R paragraphs, to the right in R2L
  4790    paragraphs) of IT's screen position.  Value is the ID of the face.  */
  4791 
  4792 static int
  4793 face_before_or_after_it_pos (struct it *it, bool before_p)
  4794 {
  4795   int face_id, limit;
  4796   ptrdiff_t next_check_charpos;
  4797   struct it it_copy;
  4798   void *it_copy_data = NULL;
  4799 
  4800   eassert (it->s == NULL);
  4801 
  4802   if (STRINGP (it->string))
  4803     {
  4804       ptrdiff_t bufpos, charpos;
  4805       int base_face_id;
  4806 
  4807       /* No face change past the end of the string (for the case we
  4808          are padding with spaces).  No face change before the string
  4809          start.  Ignore face changes before the first visible
  4810          character on this display line.  */
  4811       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
  4812           || (IT_STRING_CHARPOS (*it) == 0 && before_p)
  4813           || it->current_x <= it->first_visible_x)
  4814         return it->face_id;
  4815 
  4816       if (!it->bidi_p)
  4817         {
  4818           /* Set charpos to the position before or after IT's current
  4819              position, in the logical order, which in the non-bidi
  4820              case is the same as the visual order.  */
  4821           if (before_p)
  4822             charpos = IT_STRING_CHARPOS (*it) - 1;
  4823           else if (it->what == IT_COMPOSITION)
  4824             /* For composition, we must check the character after the
  4825                composition.  */
  4826             charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
  4827           else
  4828             charpos = IT_STRING_CHARPOS (*it) + 1;
  4829         }
  4830       else
  4831         {
  4832           /* With bidi iteration, the character before the current in
  4833              the visual order cannot be found by simple iteration,
  4834              because "reverse" reordering is not supported.  Instead,
  4835              we need to start from the string beginning and go all the
  4836              way to the current string position, remembering the
  4837              visually-previous position.  We need to start from the
  4838              string beginning for the character after the current as
  4839              well, since the iterator state in IT may have been
  4840              pushed, and the bidi cache is no longer coherent with the
  4841              string's text.  */
  4842           SAVE_IT (it_copy, *it, it_copy_data);
  4843           IT_STRING_CHARPOS (it_copy) = 0;
  4844           bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
  4845           it_copy.bidi_it.scan_dir = 0;
  4846 
  4847           do
  4848             {
  4849               charpos = it_copy.bidi_it.charpos;
  4850               if (charpos >= SCHARS (it->string))
  4851                 break;
  4852               bidi_move_to_visually_next (&it_copy.bidi_it);
  4853             }
  4854           while (it_copy.bidi_it.charpos != IT_STRING_CHARPOS (*it));
  4855 
  4856           if (!before_p)
  4857             {
  4858               /* Set charpos to the string position of the character
  4859                  that comes after IT's current position in the visual
  4860                  order.  */
  4861               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  4862               /* If this is the first string character,
  4863                  bidi_move_to_visually_next will deliver character at
  4864                  current position without moving, so we need to enlarge N.  */
  4865               if (it_copy.bidi_it.first_elt)
  4866                 n++;
  4867               while (n--)
  4868                 bidi_move_to_visually_next (&it_copy.bidi_it);
  4869 
  4870               charpos = it_copy.bidi_it.charpos;
  4871             }
  4872 
  4873           RESTORE_IT (it, it, it_copy_data);
  4874         }
  4875       eassert (0 <= charpos && charpos <= SCHARS (it->string));
  4876 
  4877       if (it->current.overlay_string_index >= 0)
  4878         bufpos = IT_CHARPOS (*it);
  4879       else
  4880         bufpos = 0;
  4881 
  4882       base_face_id = underlying_face_id (it);
  4883 
  4884       /* Get the face for ASCII, or unibyte.  */
  4885       face_id = face_at_string_position (it->w, it->string, charpos,
  4886                                          bufpos, &next_check_charpos,
  4887                                          base_face_id, false, 0);
  4888 
  4889       /* Correct the face for charsets different from ASCII.  Do it
  4890          for the multibyte case only.  The face returned above is
  4891          suitable for unibyte text if IT->string is unibyte.  */
  4892       if (STRING_MULTIBYTE (it->string))
  4893         {
  4894           struct text_pos pos1 = string_pos (charpos, it->string);
  4895           const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
  4896           struct face *face = FACE_FROM_ID (it->f, face_id);
  4897           int len, c = check_char_and_length (p, &len);
  4898           face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
  4899         }
  4900     }
  4901   else
  4902     {
  4903       struct text_pos pos;
  4904 
  4905       if ((IT_CHARPOS (*it) >= ZV && !before_p)
  4906           || (IT_CHARPOS (*it) <= BEGV && before_p))
  4907         return it->face_id;
  4908 
  4909       limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
  4910       pos = it->current.pos;
  4911 
  4912       if (!it->bidi_p)
  4913         {
  4914           if (before_p)
  4915             DEC_TEXT_POS (pos, it->multibyte_p);
  4916           else
  4917             {
  4918               if (it->what == IT_COMPOSITION)
  4919                 {
  4920                   /* For composition, we must check the position after
  4921                      the composition.  */
  4922                   pos.charpos += it->cmp_it.nchars;
  4923                   pos.bytepos += it->len;
  4924                 }
  4925               else
  4926                 INC_TEXT_POS (pos, it->multibyte_p);
  4927             }
  4928         }
  4929       else
  4930         {
  4931           if (before_p)
  4932             {
  4933               int current_x;
  4934 
  4935               /* With bidi iteration, the character before the current
  4936                  in the visual order cannot be found by simple
  4937                  iteration, because "reverse" reordering is not
  4938                  supported.  Instead, we need to use the move_it_*
  4939                  family of functions, and move to the previous
  4940                  character starting from the beginning of the visual
  4941                  line.  */
  4942               /* Ignore face changes before the first visible
  4943                  character on this display line.  */
  4944               if (it->current_x <= it->first_visible_x)
  4945                 return it->face_id;
  4946               SAVE_IT (it_copy, *it, it_copy_data);
  4947               /* Implementation note: Since move_it_in_display_line
  4948                  works in the iterator geometry, and thinks the first
  4949                  character is always the leftmost, even in R2L lines,
  4950                  we don't need to distinguish between the R2L and L2R
  4951                  cases here.  */
  4952               current_x = it_copy.current_x;
  4953               move_it_vertically_backward (&it_copy, 0);
  4954               move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
  4955               pos = it_copy.current.pos;
  4956               RESTORE_IT (it, it, it_copy_data);
  4957             }
  4958           else
  4959             {
  4960               /* Set charpos to the buffer position of the character
  4961                  that comes after IT's current position in the visual
  4962                  order.  */
  4963               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  4964 
  4965               it_copy = *it;
  4966               /* If this is the first display element,
  4967                  bidi_move_to_visually_next will deliver character at
  4968                  current position without moving, so we need to enlarge N.  */
  4969               if (it->bidi_it.first_elt)
  4970                 n++;
  4971               while (n--)
  4972                 bidi_move_to_visually_next (&it_copy.bidi_it);
  4973 
  4974               SET_TEXT_POS (pos,
  4975                             it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
  4976             }
  4977         }
  4978       eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
  4979 
  4980       /* Determine face for CHARSET_ASCII, or unibyte.  */
  4981       face_id = face_at_buffer_position (it->w,
  4982                                          CHARPOS (pos),
  4983                                          &next_check_charpos,
  4984                                          limit, false, -1, 0);
  4985 
  4986       /* Correct the face for charsets different from ASCII.  Do it
  4987          for the multibyte case only.  The face returned above is
  4988          suitable for unibyte text if current_buffer is unibyte.  */
  4989       if (it->multibyte_p)
  4990         {
  4991           int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
  4992           struct face *face = FACE_FROM_ID (it->f, face_id);
  4993           face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
  4994         }
  4995     }
  4996 
  4997   return face_id;
  4998 }
  4999 
  5000 
  5001 
  5002 /***********************************************************************
  5003                             Invisible text
  5004  ***********************************************************************/
  5005 
  5006 /* Set up iterator IT from invisible properties at its current
  5007    position.  Called from handle_stop.  */
  5008 
  5009 static enum prop_handled
  5010 handle_invisible_prop (struct it *it)
  5011 {
  5012   enum prop_handled handled = HANDLED_NORMALLY;
  5013   int invis;
  5014   Lisp_Object prop;
  5015 
  5016   if (STRINGP (it->string))
  5017     {
  5018       Lisp_Object end_charpos, limit;
  5019 
  5020       /* Get the value of the invisible text property at the
  5021          current position.  Value will be nil if there is no such
  5022          property.  */
  5023       end_charpos = make_fixnum (IT_STRING_CHARPOS (*it));
  5024       prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5025       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5026 
  5027       if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
  5028         {
  5029           /* Record whether we have to display an ellipsis for the
  5030              invisible text.  */
  5031           bool display_ellipsis_p = (invis == 2);
  5032           ptrdiff_t len, endpos;
  5033 
  5034           handled = HANDLED_RECOMPUTE_PROPS;
  5035 
  5036           /* Get the position at which the next visible text can be
  5037              found in IT->string, if any.  */
  5038           endpos = len = SCHARS (it->string);
  5039           XSETINT (limit, len);
  5040           do
  5041             {
  5042               end_charpos
  5043                 = Fnext_single_property_change (end_charpos, Qinvisible,
  5044                                                 it->string, limit);
  5045               /* Since LIMIT is always an integer, so should be the
  5046                  value returned by Fnext_single_property_change.  */
  5047               eassert (FIXNUMP (end_charpos));
  5048               if (FIXNUMP (end_charpos))
  5049                 {
  5050                   endpos = XFIXNAT (end_charpos);
  5051                   prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5052                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5053                   if (invis == 2)
  5054                     display_ellipsis_p = true;
  5055                 }
  5056               else /* Should never happen; but if it does, exit the loop.  */
  5057                 endpos = len;
  5058             }
  5059           while (invis != 0 && endpos < len);
  5060 
  5061           if (display_ellipsis_p)
  5062             it->ellipsis_p = true;
  5063 
  5064           if (endpos < len)
  5065             {
  5066               /* Text at END_CHARPOS is visible.  Move IT there.  */
  5067               struct text_pos old;
  5068               ptrdiff_t oldpos;
  5069 
  5070               old = it->current.string_pos;
  5071               oldpos = CHARPOS (old);
  5072               if (it->bidi_p)
  5073                 {
  5074                   if (it->bidi_it.first_elt
  5075                       && it->bidi_it.charpos < SCHARS (it->string))
  5076                     bidi_paragraph_init (it->paragraph_embedding,
  5077                                          &it->bidi_it, true);
  5078                   /* Bidi-iterate out of the invisible text.  */
  5079                   do
  5080                     {
  5081                       bidi_move_to_visually_next (&it->bidi_it);
  5082                     }
  5083                   while (oldpos <= it->bidi_it.charpos
  5084                          && it->bidi_it.charpos < endpos
  5085                          && it->bidi_it.charpos < it->bidi_it.string.schars);
  5086 
  5087                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  5088                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  5089                   if (IT_CHARPOS (*it) >= endpos)
  5090                     it->prev_stop = endpos;
  5091                 }
  5092               else
  5093                 {
  5094                   IT_STRING_CHARPOS (*it) = endpos;
  5095                   compute_string_pos (&it->current.string_pos, old, it->string);
  5096                 }
  5097             }
  5098           else
  5099             {
  5100               /* The rest of the string is invisible.  If this is an
  5101                  overlay string, proceed with the next overlay string
  5102                  or whatever comes and return a character from there.  */
  5103               if (it->current.overlay_string_index >= 0
  5104                   && !display_ellipsis_p)
  5105                 {
  5106                   next_overlay_string (it);
  5107                   /* Don't check for overlay strings when we just
  5108                      finished processing them.  */
  5109                   handled = HANDLED_OVERLAY_STRING_CONSUMED;
  5110                 }
  5111               else
  5112                 {
  5113                   IT_STRING_CHARPOS (*it) = SCHARS (it->string);
  5114                   IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
  5115                 }
  5116             }
  5117         }
  5118     }
  5119   else
  5120     {
  5121       ptrdiff_t newpos, next_stop, start_charpos, tem;
  5122       Lisp_Object pos, overlay;
  5123 
  5124       /* First of all, is there invisible text at this position?  */
  5125       tem = start_charpos = IT_CHARPOS (*it);
  5126       pos = make_fixnum (tem);
  5127       prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
  5128                                             &overlay);
  5129       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5130 
  5131       /* If we are on invisible text, skip over it.  */
  5132       if (invis != 0 && start_charpos < it->end_charpos)
  5133         {
  5134           /* Record whether we have to display an ellipsis for the
  5135              invisible text.  */
  5136           bool display_ellipsis_p = invis == 2;
  5137 
  5138           handled = HANDLED_RECOMPUTE_PROPS;
  5139 
  5140           /* Loop skipping over invisible text.  The loop is left at
  5141              ZV or with IT on the first char being visible again.  */
  5142           do
  5143             {
  5144               /* Try to skip some invisible text.  Return value is the
  5145                  position reached which can be equal to where we start
  5146                  if there is nothing invisible there.  This skips both
  5147                  over invisible text properties and overlays with
  5148                  invisible property.  */
  5149               newpos = skip_invisible (tem, &next_stop, ZV, it->window);
  5150 
  5151               /* If we skipped nothing at all we weren't at invisible
  5152                  text in the first place.  If everything to the end of
  5153                  the buffer was skipped, end the loop.  */
  5154               if (newpos == tem || newpos >= ZV)
  5155                 invis = 0;
  5156               else
  5157                 {
  5158                   /* We skipped some characters but not necessarily
  5159                      all there are.  Check if we ended up on visible
  5160                      text.  Fget_char_property returns the property of
  5161                      the char before the given position, i.e. if we
  5162                      get invis = 0, this means that the char at
  5163                      newpos is visible.  */
  5164                   pos = make_fixnum (newpos);
  5165                   prop = Fget_char_property (pos, Qinvisible, it->window);
  5166                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5167                 }
  5168 
  5169               /* If we ended up on invisible text, proceed to
  5170                  skip starting with next_stop.  */
  5171               if (invis != 0)
  5172                 tem = next_stop;
  5173 
  5174               /* If there are adjacent invisible texts, don't lose the
  5175                  second one's ellipsis.  */
  5176               if (invis == 2)
  5177                 display_ellipsis_p = true;
  5178             }
  5179           while (invis != 0);
  5180 
  5181           /* The position newpos is now either ZV or on visible text.  */
  5182           if (it->bidi_p)
  5183             {
  5184               ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
  5185               bool on_newline
  5186                 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
  5187               bool after_newline
  5188                 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
  5189 
  5190               /* If the invisible text ends on a newline or on a
  5191                  character after a newline, we can avoid the costly,
  5192                  character by character, bidi iteration to NEWPOS, and
  5193                  instead simply reseat the iterator there.  That's
  5194                  because all bidi reordering information is tossed at
  5195                  the newline.  This is a big win for modes that hide
  5196                  complete lines, like Outline, Org, etc.  */
  5197               if (on_newline || after_newline)
  5198                 {
  5199                   struct text_pos tpos;
  5200                   bidi_dir_t pdir = it->bidi_it.paragraph_dir;
  5201 
  5202                   SET_TEXT_POS (tpos, newpos, bpos);
  5203                   reseat_1 (it, tpos, false);
  5204                   /* If we reseat on a newline/ZV, we need to prep the
  5205                      bidi iterator for advancing to the next character
  5206                      after the newline/EOB, keeping the current paragraph
  5207                      direction (so that PRODUCE_GLYPHS does TRT wrt
  5208                      prepending/appending glyphs to a glyph row).  */
  5209                   if (on_newline)
  5210                     {
  5211                       it->bidi_it.first_elt = false;
  5212                       it->bidi_it.paragraph_dir = pdir;
  5213                       it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
  5214                       it->bidi_it.nchars = 1;
  5215                       it->bidi_it.ch_len = 1;
  5216                     }
  5217                 }
  5218               else      /* Must use the slow method.  */
  5219                 {
  5220                   /* With bidi iteration, the region of invisible text
  5221                      could start and/or end in the middle of a
  5222                      non-base embedding level.  Therefore, we need to
  5223                      skip invisible text using the bidi iterator,
  5224                      starting at IT's current position, until we find
  5225                      ourselves outside of the invisible text.
  5226                      Skipping invisible text _after_ bidi iteration
  5227                      avoids affecting the visual order of the
  5228                      displayed text when invisible properties are
  5229                      added or removed.  */
  5230                   if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
  5231                     {
  5232                       /* If we were `reseat'ed to a new paragraph,
  5233                          determine the paragraph base direction.  We
  5234                          need to do it now because
  5235                          next_element_from_buffer may not have a
  5236                          chance to do it, if we are going to skip any
  5237                          text at the beginning, which resets the
  5238                          FIRST_ELT flag.  */
  5239                       bidi_paragraph_init (it->paragraph_embedding,
  5240                                            &it->bidi_it, true);
  5241                     }
  5242                   do
  5243                     {
  5244                       bidi_move_to_visually_next (&it->bidi_it);
  5245                     }
  5246                   while (it->stop_charpos <= it->bidi_it.charpos
  5247                          && it->bidi_it.charpos < newpos);
  5248                   IT_CHARPOS (*it) = it->bidi_it.charpos;
  5249                   IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  5250                   /* If we overstepped NEWPOS, record its position in
  5251                      the iterator, so that we skip invisible text if
  5252                      later the bidi iteration lands us in the
  5253                      invisible region again. */
  5254                   if (IT_CHARPOS (*it) >= newpos)
  5255                     it->prev_stop = newpos;
  5256                 }
  5257             }
  5258           else
  5259             {
  5260               IT_CHARPOS (*it) = newpos;
  5261               IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
  5262             }
  5263 
  5264           if (display_ellipsis_p)
  5265             {
  5266               /* Make sure that the glyphs of the ellipsis will get
  5267                  correct `charpos' values.  If we would not update
  5268                  it->position here, the glyphs would belong to the
  5269                  last visible character _before_ the invisible
  5270                  text, which confuses `set_cursor_from_row'.
  5271 
  5272                  We use the last invisible position instead of the
  5273                  first because this way the cursor is always drawn on
  5274                  the first "." of the ellipsis, whenever PT is inside
  5275                  the invisible text.  Otherwise the cursor would be
  5276                  placed _after_ the ellipsis when the point is after the
  5277                  first invisible character.  */
  5278               if (!STRINGP (it->object))
  5279                 {
  5280                   it->position.charpos = newpos - 1;
  5281                   it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
  5282                 }
  5283             }
  5284 
  5285           /* If there are before-strings at the start of invisible
  5286              text, and the text is invisible because of a text
  5287              property, arrange to show before-strings because 20.x did
  5288              it that way.  (If the text is invisible because of an
  5289              overlay property instead of a text property, this is
  5290              already handled in the overlay code.)  */
  5291           if (NILP (overlay)
  5292               && get_overlay_strings (it, it->stop_charpos))
  5293             {
  5294               handled = HANDLED_RECOMPUTE_PROPS;
  5295               if (it->sp > 0)
  5296                 {
  5297                   it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
  5298                   /* The call to get_overlay_strings above recomputes
  5299                      it->stop_charpos, but it only considers changes
  5300                      in properties and overlays beyond iterator's
  5301                      current position.  This causes us to miss changes
  5302                      that happen exactly where the invisible property
  5303                      ended.  So we play it safe here and force the
  5304                      iterator to check for potential stop positions
  5305                      immediately after the invisible text.  Note that
  5306                      if get_overlay_strings returns true, it
  5307                      normally also pushed the iterator stack, so we
  5308                      need to update the stop position in the slot
  5309                      below the current one.  */
  5310                   it->stack[it->sp - 1].stop_charpos
  5311                     = CHARPOS (it->stack[it->sp - 1].current.pos);
  5312                 }
  5313             }
  5314           else if (display_ellipsis_p)
  5315             {
  5316               it->ellipsis_p = true;
  5317               /* Let the ellipsis display before
  5318                  considering any properties of the following char.
  5319                  Fixes jasonr@gnu.org 01 Oct 07 bug.  */
  5320               handled = HANDLED_RETURN;
  5321             }
  5322         }
  5323     }
  5324 
  5325   return handled;
  5326 }
  5327 
  5328 
  5329 /* Make iterator IT return `...' next.
  5330    Replaces LEN characters from buffer.  */
  5331 
  5332 static void
  5333 setup_for_ellipsis (struct it *it, int len)
  5334 {
  5335   /* Use the display table definition for `...'.  Invalid glyphs
  5336      will be handled by the method returning elements from dpvec.  */
  5337   if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
  5338     {
  5339       struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
  5340       it->dpvec = v->contents;
  5341       it->dpend = v->contents + v->header.size;
  5342     }
  5343   else
  5344     {
  5345       /* Default `...'.  */
  5346       it->dpvec = default_invis_vector;
  5347       it->dpend = default_invis_vector + 3;
  5348     }
  5349 
  5350   it->dpvec_char_len = len;
  5351   it->current.dpvec_index = 0;
  5352   it->dpvec_face_id = -1;
  5353 
  5354   /* Use IT->saved_face_id for the ellipsis, so that it has the same
  5355      face as the preceding text.  IT->saved_face_id was set in
  5356      handle_stop to the face of the preceding character, and will be
  5357      different from IT->face_id only if the invisible text skipped in
  5358      handle_invisible_prop has some non-default face on its first
  5359      character.  We thus ignore the face of the invisible text when we
  5360      display the ellipsis.  IT's face is restored in set_iterator_to_next.  */
  5361   if (it->saved_face_id >= 0)
  5362     it->face_id = it->saved_face_id;
  5363 
  5364   /* If the ellipsis represents buffer text, it means we advanced in
  5365      the buffer, so we should no longer ignore overlay strings.  */
  5366   if (it->method == GET_FROM_BUFFER)
  5367     it->ignore_overlay_strings_at_pos_p = false;
  5368 
  5369   it->method = GET_FROM_DISPLAY_VECTOR;
  5370   it->ellipsis_p = true;
  5371 }
  5372 
  5373 
  5374 static Lisp_Object
  5375 find_display_property (Lisp_Object disp, Lisp_Object prop)
  5376 {
  5377   if (NILP (disp))
  5378     return Qnil;
  5379   /* We have a vector of display specs.  */
  5380   if (VECTORP (disp))
  5381     {
  5382       for (ptrdiff_t i = 0; i < ASIZE (disp); i++)
  5383         {
  5384           Lisp_Object elem = AREF (disp, i);
  5385           if (CONSP (elem)
  5386               && CONSP (XCDR (elem))
  5387               && EQ (XCAR (elem), prop))
  5388             return XCAR (XCDR (elem));
  5389         }
  5390       return Qnil;
  5391     }
  5392   /* We have a list of display specs.  */
  5393   else if (CONSP (disp)
  5394            && CONSP (XCAR (disp)))
  5395     {
  5396       while (!NILP (disp))
  5397         {
  5398           Lisp_Object elem = XCAR (disp);
  5399           if (CONSP (elem)
  5400               && CONSP (XCDR (elem))
  5401               && EQ (XCAR (elem), prop))
  5402             return XCAR (XCDR (elem));
  5403 
  5404           /* Check that we have a proper list before going to the next
  5405              element.  */
  5406           if (CONSP (XCDR (disp)))
  5407             disp = XCDR (disp);
  5408           else
  5409             disp = Qnil;
  5410         }
  5411       return Qnil;
  5412     }
  5413   /* A simple display spec.  */
  5414   else if (CONSP (disp)
  5415            && CONSP (XCDR (disp))
  5416            && EQ (XCAR (disp), prop))
  5417     return XCAR (XCDR (disp));
  5418   else
  5419     return Qnil;
  5420 }
  5421 
  5422 static Lisp_Object
  5423 get_display_property (ptrdiff_t bufpos, Lisp_Object prop, Lisp_Object object)
  5424 {
  5425   return find_display_property (Fget_text_property (make_fixnum (bufpos),
  5426                                                     Qdisplay, object),
  5427                                 prop);
  5428 }
  5429 
  5430 static void
  5431 display_min_width (struct it *it, ptrdiff_t bufpos,
  5432                    Lisp_Object object, Lisp_Object width_spec)
  5433 {
  5434   /* We're being called at the end of the `min-width' sequence,
  5435      probably. */
  5436   if (!NILP (it->min_width_property)
  5437       && !EQ (width_spec, it->min_width_property))
  5438     {
  5439       if (!it->glyph_row)
  5440         return;
  5441 
  5442       /* When called from display_string (i.e., the mode line),
  5443          we're being called with a string as the object, and we
  5444          may be called with many sub-strings belonging to the same
  5445          :propertize run. */
  5446       if ((bufpos == 0
  5447            && !EQ (it->min_width_property,
  5448                    get_display_property (0, Qmin_width, object)))
  5449           /* In a buffer -- check that we're really right after the
  5450              sequence of characters covered by this `min-width'.  */
  5451           || (bufpos > BEGV
  5452               && EQ (it->min_width_property,
  5453                      get_display_property (bufpos - 1, Qmin_width, object))))
  5454         {
  5455           Lisp_Object w = Qnil;
  5456           double width;
  5457 #ifdef HAVE_WINDOW_SYSTEM
  5458           if (FRAME_WINDOW_P (it->f))
  5459             {
  5460               struct font *font = NULL;
  5461               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5462               font = face->font ? face->font : FRAME_FONT (it->f);
  5463               calc_pixel_width_or_height (&width, it,
  5464                                           XCAR (it->min_width_property),
  5465                                           font, true, NULL);
  5466               width -= it->current_x - it->min_width_start;
  5467               w = list1 (make_int (width));
  5468             }
  5469           else
  5470 #endif
  5471             {
  5472               calc_pixel_width_or_height (&width, it,
  5473                                           XCAR (it->min_width_property),
  5474                                           NULL, true, NULL);
  5475               width -= (it->current_x - it->min_width_start) /
  5476                 FRAME_COLUMN_WIDTH (it->f);
  5477               w = make_int (width);
  5478             }
  5479 
  5480           /* Insert the stretch glyph.  */
  5481           it->object = list3 (Qspace, QCwidth, w);
  5482           produce_stretch_glyph (it);
  5483           if (it->area == TEXT_AREA)
  5484             it->current_x += it->pixel_width;
  5485           it->min_width_property = Qnil;
  5486         }
  5487     }
  5488 
  5489   /* We're at the start of a `min-width' sequence -- record the
  5490      position and the property, so that we can later see if we're at
  5491      the end.  */
  5492   if (CONSP (width_spec))
  5493     {
  5494       if (bufpos == BEGV
  5495           /* Mode line (see above).  */
  5496           || (bufpos == 0
  5497               && !EQ (it->min_width_property,
  5498                       get_display_property (0, Qmin_width, object)))
  5499           /* Buffer.  */
  5500           || (bufpos > BEGV
  5501               && !EQ (width_spec,
  5502                       get_display_property (bufpos - 1, Qmin_width, object))))
  5503         {
  5504           it->min_width_property = width_spec;
  5505           it->min_width_start = it->current_x;
  5506         }
  5507     }
  5508 }
  5509 
  5510 DEFUN ("get-display-property", Fget_display_property,
  5511        Sget_display_property, 2, 4, 0,
  5512        doc: /* Get the value of the `display' property PROP at POSITION.
  5513 If OBJECT, this should be a buffer or string where the property is
  5514 fetched from.  If omitted, OBJECT defaults to the current buffer.
  5515 
  5516 If PROPERTIES, look for value of PROP in PROPERTIES instead of the
  5517 properties at POSITION.  */)
  5518   (Lisp_Object position, Lisp_Object prop, Lisp_Object object,
  5519    Lisp_Object properties)
  5520 {
  5521   if (NILP (properties))
  5522     properties = Fget_text_property (position, Qdisplay, object);
  5523   else
  5524     CHECK_LIST (properties);
  5525 
  5526   return find_display_property (properties, prop);
  5527 }
  5528 
  5529 
  5530 
  5531 /***********************************************************************
  5532                             'display' property
  5533  ***********************************************************************/
  5534 
  5535 /* Set up iterator IT from `display' property at its current position.
  5536    Called from handle_stop.
  5537    We return HANDLED_RETURN if some part of the display property
  5538    overrides the display of the buffer text itself.
  5539    Otherwise we return HANDLED_NORMALLY.  */
  5540 
  5541 static enum prop_handled
  5542 handle_display_prop (struct it *it)
  5543 {
  5544   Lisp_Object propval, object, overlay;
  5545   struct text_pos *position;
  5546   ptrdiff_t bufpos;
  5547   /* Nonzero if some property replaces the display of the text itself.  */
  5548   int display_replaced = 0;
  5549 
  5550   if (STRINGP (it->string))
  5551     {
  5552       object = it->string;
  5553       position = &it->current.string_pos;
  5554       bufpos = CHARPOS (it->current.pos);
  5555     }
  5556   else
  5557     {
  5558       XSETWINDOW (object, it->w);
  5559       position = &it->current.pos;
  5560       bufpos = CHARPOS (*position);
  5561     }
  5562 
  5563   /* Reset those iterator values set from display property values.  */
  5564   it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
  5565   it->space_width = Qnil;
  5566   it->font_height = Qnil;
  5567   it->voffset = 0;
  5568 
  5569   /* We don't support recursive `display' properties, i.e. string
  5570      values that have a string `display' property, that have a string
  5571      `display' property etc.  */
  5572   if (!it->string_from_display_prop_p)
  5573     it->area = TEXT_AREA;
  5574 
  5575   propval = get_char_property_and_overlay (make_fixnum (position->charpos),
  5576                                            Qdisplay, object, &overlay);
  5577 
  5578   /* Rest of the code must have OBJECT be either a string or a buffer.  */
  5579   if (!STRINGP (it->string))
  5580     object = it->w->contents;
  5581 
  5582   /* Handle min-width ends. */
  5583   if (!NILP (it->min_width_property)
  5584       && NILP (find_display_property (propval, Qmin_width)))
  5585     display_min_width (it, bufpos, object, Qnil);
  5586 
  5587   if (NILP (propval))
  5588     return HANDLED_NORMALLY;
  5589   /* Now OVERLAY is the overlay that gave us this property, or nil
  5590      if it was a text property.  */
  5591 
  5592   display_replaced = handle_display_spec (it, propval, object, overlay,
  5593                                           position, bufpos,
  5594                                           FRAME_WINDOW_P (it->f));
  5595   return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
  5596 }
  5597 
  5598 /* Subroutine of handle_display_prop.  Returns non-zero if the display
  5599    specification in SPEC is a replacing specification, i.e. it would
  5600    replace the text covered by `display' property with something else,
  5601    such as an image or a display string.  If SPEC includes any kind or
  5602    `(space ...) specification, the value is 2; this is used by
  5603    compute_display_string_pos, which see.
  5604 
  5605    See handle_single_display_spec for documentation of arguments.
  5606    FRAME_WINDOW_P is true if the window being redisplayed is on a
  5607    GUI frame; this argument is used only if IT is NULL, see below.
  5608 
  5609    IT can be NULL, if this is called by the bidi reordering code
  5610    through compute_display_string_pos, which see.  In that case, this
  5611    function only examines SPEC, but does not otherwise "handle" it, in
  5612    the sense that it doesn't set up members of IT from the display
  5613    spec.  */
  5614 static int
  5615 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5616                      Lisp_Object overlay, struct text_pos *position,
  5617                      ptrdiff_t bufpos, bool frame_window_p)
  5618 {
  5619   int replacing = 0;
  5620   bool enable_eval = true;
  5621 
  5622   /* Support (disable-eval PROP) which is used by enriched.el.  */
  5623   if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
  5624     {
  5625       enable_eval = false;
  5626       spec = CONSP (XCDR (spec)) ? XCAR (XCDR (spec)) : Qnil;
  5627     }
  5628 
  5629   if (CONSP (spec)
  5630       /* Simple specifications.  */
  5631       && !EQ (XCAR (spec), Qimage)
  5632 #ifdef HAVE_XWIDGETS
  5633       && !EQ (XCAR (spec), Qxwidget)
  5634 #endif
  5635       && !EQ (XCAR (spec), Qspace)
  5636       && !EQ (XCAR (spec), Qwhen)
  5637       && !EQ (XCAR (spec), Qslice)
  5638       && !EQ (XCAR (spec), Qspace_width)
  5639       && !EQ (XCAR (spec), Qheight)
  5640       && !EQ (XCAR (spec), Qraise)
  5641       /* Marginal area specifications.  */
  5642       && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
  5643       && !EQ (XCAR (spec), Qleft_fringe)
  5644       && !EQ (XCAR (spec), Qright_fringe)
  5645       && !EQ (XCAR (spec), Qmin_width)
  5646       && !NILP (XCAR (spec)))
  5647     {
  5648       for (; CONSP (spec); spec = XCDR (spec))
  5649         {
  5650           int rv = handle_single_display_spec (it, XCAR (spec), object,
  5651                                                overlay, position, bufpos,
  5652                                                replacing, frame_window_p,
  5653                                                enable_eval);
  5654           if (rv != 0)
  5655             {
  5656               replacing = rv;
  5657               /* If some text in a string is replaced, `position' no
  5658                  longer points to the position of `object'.  */
  5659               if (!it || STRINGP (object))
  5660                 break;
  5661             }
  5662         }
  5663     }
  5664   else if (VECTORP (spec))
  5665     {
  5666       ptrdiff_t i;
  5667       for (i = 0; i < ASIZE (spec); ++i)
  5668         {
  5669           int rv = handle_single_display_spec (it, AREF (spec, i), object,
  5670                                                overlay, position, bufpos,
  5671                                                replacing, frame_window_p,
  5672                                                enable_eval);
  5673           if (rv != 0)
  5674             {
  5675               replacing = rv;
  5676               /* If some text in a string is replaced, `position' no
  5677                  longer points to the position of `object'.  */
  5678               if (!it || STRINGP (object))
  5679                 break;
  5680             }
  5681         }
  5682     }
  5683   else
  5684     replacing = handle_single_display_spec (it, spec, object, overlay, position,
  5685                                             bufpos, 0, frame_window_p,
  5686                                             enable_eval);
  5687   return replacing;
  5688 }
  5689 
  5690 /* Value is the position of the end of the `display' property starting
  5691    at START_POS in OBJECT.  */
  5692 
  5693 static struct text_pos
  5694 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
  5695 {
  5696   Lisp_Object end;
  5697   struct text_pos end_pos;
  5698 
  5699   end = Fnext_single_char_property_change (make_fixnum (CHARPOS (start_pos)),
  5700                                            Qdisplay, object, Qnil);
  5701   CHARPOS (end_pos) = XFIXNAT (end);
  5702   if (STRINGP (object))
  5703     compute_string_pos (&end_pos, start_pos, it->string);
  5704   else
  5705     BYTEPOS (end_pos) = CHAR_TO_BYTE (XFIXNAT (end));
  5706 
  5707   return end_pos;
  5708 }
  5709 
  5710 
  5711 /* Set up IT from a single `display' property specification SPEC.  OBJECT
  5712    is the object in which the `display' property was found.  *POSITION
  5713    is the position in OBJECT at which the `display' property was found.
  5714    BUFPOS is the buffer position of OBJECT (different from POSITION if
  5715    OBJECT is not a buffer).  DISPLAY_REPLACED non-zero means that we
  5716    previously saw a display specification which already replaced text
  5717    display with something else, for example an image; we ignore such
  5718    properties after the first one has been processed.
  5719 
  5720    OVERLAY is the overlay this `display' property came from,
  5721    or nil if it was a text property.
  5722 
  5723    If SPEC is a `space' or `image' specification, and in some other
  5724    cases too, set *POSITION to the position where the `display'
  5725    property ends.
  5726 
  5727    If IT is NULL, only examine the property specification in SPEC, but
  5728    don't set up IT.  In that case, FRAME_WINDOW_P means SPEC
  5729    is intended to be displayed in a window on a GUI frame.
  5730 
  5731    Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
  5732 
  5733    Value is non-zero if something was found which replaces the display
  5734    of buffer or string text.  */
  5735 
  5736 static int
  5737 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5738                             Lisp_Object overlay, struct text_pos *position,
  5739                             ptrdiff_t bufpos, int display_replaced,
  5740                             bool frame_window_p, bool enable_eval_p)
  5741 {
  5742   Lisp_Object form;
  5743   Lisp_Object location, value;
  5744   struct text_pos start_pos = *position;
  5745   void *itdata = NULL;
  5746 
  5747   /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
  5748      If the result is non-nil, use VALUE instead of SPEC.  */
  5749   form = Qt;
  5750   if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
  5751     {
  5752       spec = XCDR (spec);
  5753       if (!CONSP (spec))
  5754         return 0;
  5755       form = XCAR (spec);
  5756       spec = XCDR (spec);
  5757     }
  5758 
  5759   if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
  5760     form = Qnil;
  5761   if (!NILP (form) && !EQ (form, Qt))
  5762     {
  5763       specpdl_ref count = SPECPDL_INDEX ();
  5764 
  5765       /* Bind `object' to the object having the `display' property, a
  5766          buffer or string.  Bind `position' to the position in the
  5767          object where the property was found, and `buffer-position'
  5768          to the current position in the buffer.  */
  5769 
  5770       if (NILP (object))
  5771         XSETBUFFER (object, current_buffer);
  5772       specbind (Qobject, object);
  5773       specbind (Qposition, make_fixnum (CHARPOS (*position)));
  5774       specbind (Qbuffer_position, make_fixnum (bufpos));
  5775       /* Save and restore the bidi cache, since FORM could be crazy
  5776          enough to re-enter redisplay, e.g., by calling 'message'.  */
  5777       itdata = bidi_shelve_cache ();
  5778       form = safe_eval (form);
  5779       bidi_unshelve_cache (itdata, false);
  5780       form = unbind_to (count, form);
  5781     }
  5782 
  5783   if (NILP (form))
  5784     return 0;
  5785 
  5786   /* Handle `(height HEIGHT)' specifications.  */
  5787   if (CONSP (spec)
  5788       && EQ (XCAR (spec), Qheight)
  5789       && CONSP (XCDR (spec)))
  5790     {
  5791       if (it)
  5792         {
  5793           if (!FRAME_WINDOW_P (it->f))
  5794             return 0;
  5795 
  5796           it->font_height = XCAR (XCDR (spec));
  5797           if (!NILP (it->font_height))
  5798             {
  5799               int new_height = -1;
  5800 
  5801               if (CONSP (it->font_height)
  5802                   && (EQ (XCAR (it->font_height), Qplus)
  5803                       || EQ (XCAR (it->font_height), Qminus))
  5804                   && CONSP (XCDR (it->font_height))
  5805                   && RANGED_FIXNUMP (0, XCAR (XCDR (it->font_height)), INT_MAX))
  5806                 {
  5807                   /* `(+ N)' or `(- N)' where N is an integer.  */
  5808                   int steps = XFIXNUM (XCAR (XCDR (it->font_height)));
  5809                   if (EQ (XCAR (it->font_height), Qplus))
  5810                     steps = - steps;
  5811                   it->face_id = smaller_face (it->f, it->face_id, steps);
  5812                 }
  5813               else if (FUNCTIONP (it->font_height) && enable_eval_p)
  5814                 {
  5815                   /* Call function with current height as argument.
  5816                      Value is the new height.  */
  5817                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5818                   Lisp_Object height;
  5819                   itdata = bidi_shelve_cache ();
  5820                   height = safe_call1 (it->font_height,
  5821                                        face->lface[LFACE_HEIGHT_INDEX]);
  5822                   bidi_unshelve_cache (itdata, false);
  5823                   if (NUMBERP (height))
  5824                     new_height = XFLOATINT (height);
  5825                 }
  5826               else if (NUMBERP (it->font_height))
  5827                 {
  5828                   /* Value is a multiple of the canonical char height.  */
  5829                   struct face *f;
  5830 
  5831                   f = FACE_FROM_ID (it->f,
  5832                                     lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID));
  5833                   new_height = (XFLOATINT (it->font_height)
  5834                                 * XFIXNUM (f->lface[LFACE_HEIGHT_INDEX]));
  5835                 }
  5836               else if (enable_eval_p)
  5837                 {
  5838                   /* Evaluate IT->font_height with `height' bound to the
  5839                      current specified height to get the new height.  */
  5840                   specpdl_ref count = SPECPDL_INDEX ();
  5841                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5842 
  5843                   specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
  5844                   itdata = bidi_shelve_cache ();
  5845                   value = safe_eval (it->font_height);
  5846                   bidi_unshelve_cache (itdata, false);
  5847                   value = unbind_to (count, value);
  5848 
  5849                   if (NUMBERP (value))
  5850                     new_height = XFLOATINT (value);
  5851                 }
  5852 
  5853               if (new_height > 0)
  5854                 it->face_id = face_with_height (it->f, it->face_id, new_height);
  5855             }
  5856         }
  5857 
  5858       return 0;
  5859     }
  5860 
  5861   /* Handle `(space-width WIDTH)'.  */
  5862   if (CONSP (spec)
  5863       && EQ (XCAR (spec), Qspace_width)
  5864       && CONSP (XCDR (spec)))
  5865     {
  5866       if (it)
  5867         {
  5868           if (!FRAME_WINDOW_P (it->f))
  5869             return 0;
  5870 
  5871           value = XCAR (XCDR (spec));
  5872           if (NUMBERP (value) && XFLOATINT (value) > 0)
  5873             it->space_width = value;
  5874         }
  5875 
  5876       return 0;
  5877     }
  5878 
  5879   /* Handle `(min-width (WIDTH))'.  */
  5880   if (CONSP (spec)
  5881       && EQ (XCAR (spec), Qmin_width)
  5882       && CONSP (XCDR (spec))
  5883       && CONSP (XCAR (XCDR (spec))))
  5884     {
  5885       if (it)
  5886         display_min_width (it, bufpos, object, XCAR (XCDR (spec)));
  5887       return 0;
  5888     }
  5889 
  5890   /* Handle `(slice X Y WIDTH HEIGHT)'.  */
  5891   if (CONSP (spec)
  5892       && EQ (XCAR (spec), Qslice))
  5893     {
  5894       Lisp_Object tem;
  5895 
  5896       if (it)
  5897         {
  5898           if (!FRAME_WINDOW_P (it->f))
  5899             return 0;
  5900 
  5901           if (tem = XCDR (spec), CONSP (tem))
  5902             {
  5903               it->slice.x = XCAR (tem);
  5904               if (tem = XCDR (tem), CONSP (tem))
  5905                 {
  5906                   it->slice.y = XCAR (tem);
  5907                   if (tem = XCDR (tem), CONSP (tem))
  5908                     {
  5909                       it->slice.width = XCAR (tem);
  5910                       if (tem = XCDR (tem), CONSP (tem))
  5911                         it->slice.height = XCAR (tem);
  5912                     }
  5913                 }
  5914             }
  5915         }
  5916 
  5917       return 0;
  5918     }
  5919 
  5920   /* Handle `(raise FACTOR)'.  */
  5921   if (CONSP (spec)
  5922       && EQ (XCAR (spec), Qraise)
  5923       && CONSP (XCDR (spec)))
  5924     {
  5925       if (it)
  5926         {
  5927           if (!FRAME_WINDOW_P (it->f))
  5928             return 0;
  5929 
  5930 #ifdef HAVE_WINDOW_SYSTEM
  5931           value = XCAR (XCDR (spec));
  5932           if (NUMBERP (value))
  5933             {
  5934               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5935               it->voffset = - (XFLOATINT (value)
  5936                                * (normal_char_height (face->font, -1)));
  5937             }
  5938 #endif /* HAVE_WINDOW_SYSTEM */
  5939         }
  5940 
  5941       return 0;
  5942     }
  5943 
  5944   /* Don't handle the other kinds of display specifications
  5945      inside a string that we got from a `display' property.  */
  5946   if (it && it->string_from_display_prop_p)
  5947     return 0;
  5948 
  5949   /* Characters having this form of property are not displayed, so
  5950      we have to find the end of the property.  */
  5951   if (it)
  5952     {
  5953       start_pos = *position;
  5954       *position = display_prop_end (it, object, start_pos);
  5955       /* If the display property comes from an overlay, don't consider
  5956          any potential stop_charpos values before the end of that
  5957          overlay.  Since display_prop_end will happily find another
  5958          'display' property coming from some other overlay or text
  5959          property on buffer positions before this overlay's end, we
  5960          need to ignore them, or else we risk displaying this
  5961          overlay's display string/image twice.  */
  5962       if (!NILP (overlay))
  5963         {
  5964           ptrdiff_t ovendpos = OVERLAY_END (overlay);
  5965 
  5966           /* Some borderline-sane Lisp might call us with the current
  5967              buffer narrowed so that overlay-end is outside the
  5968              POINT_MIN..POINT_MAX region, which will then cause
  5969              various assertion violations and crashes down the road,
  5970              starting with pop_it when it will attempt to use POSITION
  5971              set below.  Prevent that.  */
  5972           ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
  5973 
  5974           if (ovendpos > CHARPOS (*position))
  5975             SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
  5976         }
  5977     }
  5978   value = Qnil;
  5979 
  5980   /* Stop the scan at that end position--we assume that all
  5981      text properties change there.  */
  5982   if (it)
  5983     it->stop_charpos = position->charpos;
  5984 
  5985   /* Handle `(left-fringe BITMAP [FACE])'
  5986      and `(right-fringe BITMAP [FACE])'.  */
  5987   if (CONSP (spec)
  5988       && (EQ (XCAR (spec), Qleft_fringe)
  5989           || EQ (XCAR (spec), Qright_fringe))
  5990       && CONSP (XCDR (spec)))
  5991     {
  5992       if (it)
  5993         {
  5994           if (!FRAME_WINDOW_P (it->f))
  5995             /* If we return here, POSITION has been advanced
  5996                across the text with this property.  */
  5997             {
  5998               /* Synchronize the bidi iterator with POSITION.  This is
  5999                  needed because we are not going to push the iterator
  6000                  on behalf of this display property, so there will be
  6001                  no pop_it call to do this synchronization for us.  */
  6002               if (it->bidi_p)
  6003                 {
  6004                   it->position = *position;
  6005                   iterate_out_of_display_property (it);
  6006                   *position = it->position;
  6007                 }
  6008               return 1;
  6009             }
  6010         }
  6011       else if (!frame_window_p)
  6012         return 1;
  6013 
  6014 #ifdef HAVE_WINDOW_SYSTEM
  6015       value = XCAR (XCDR (spec));
  6016       int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
  6017       if (! fringe_bitmap)
  6018         /* If we return here, POSITION has been advanced
  6019            across the text with this property.  */
  6020         {
  6021           if (it && it->bidi_p)
  6022             {
  6023               it->position = *position;
  6024               iterate_out_of_display_property (it);
  6025               *position = it->position;
  6026             }
  6027           return 1;
  6028         }
  6029 
  6030       if (it)
  6031         {
  6032           int face_id = lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
  6033 
  6034           if (CONSP (XCDR (XCDR (spec))))
  6035             {
  6036               Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
  6037               int face_id2;
  6038               /* Don't allow quitting from lookup_derived_face, for when
  6039                  we are displaying a non-selected window, and the buffer's
  6040                  point was temporarily moved to the window-point.  */
  6041               specpdl_ref count1 = SPECPDL_INDEX ();
  6042               specbind (Qinhibit_quit, Qt);
  6043               face_id2 = lookup_derived_face (it->w, it->f, face_name,
  6044                                               FRINGE_FACE_ID, false);
  6045               unbind_to (count1, Qnil);
  6046               if (face_id2 >= 0)
  6047                 face_id = face_id2;
  6048             }
  6049 
  6050           /* Save current settings of IT so that we can restore them
  6051              when we are finished with the glyph property value.  */
  6052           push_it (it, position);
  6053 
  6054           it->area = TEXT_AREA;
  6055           it->what = IT_IMAGE;
  6056           it->image_id = -1; /* no image */
  6057           it->position = start_pos;
  6058           it->object = NILP (object) ? it->w->contents : object;
  6059           it->method = GET_FROM_IMAGE;
  6060           it->from_overlay = Qnil;
  6061           it->face_id = face_id;
  6062           it->from_disp_prop_p = true;
  6063 
  6064           /* Say that we haven't consumed the characters with
  6065              `display' property yet.  The call to pop_it in
  6066              set_iterator_to_next will clean this up.  */
  6067           *position = start_pos;
  6068 
  6069           if (EQ (XCAR (spec), Qleft_fringe))
  6070             {
  6071               it->left_user_fringe_bitmap = fringe_bitmap;
  6072               it->left_user_fringe_face_id = face_id;
  6073             }
  6074           else
  6075             {
  6076               it->right_user_fringe_bitmap = fringe_bitmap;
  6077               it->right_user_fringe_face_id = face_id;
  6078             }
  6079         }
  6080 #endif /* HAVE_WINDOW_SYSTEM */
  6081       return 1;
  6082     }
  6083 
  6084   /* Prepare to handle `((margin left-margin) ...)',
  6085      `((margin right-margin) ...)' and `((margin nil) ...)'
  6086      prefixes for display specifications.  */
  6087   location = Qunbound;
  6088   if (CONSP (spec) && CONSP (XCAR (spec)))
  6089     {
  6090       Lisp_Object tem;
  6091 
  6092       value = XCDR (spec);
  6093       if (CONSP (value))
  6094         value = XCAR (value);
  6095 
  6096       tem = XCAR (spec);
  6097       if (EQ (XCAR (tem), Qmargin)
  6098           && (tem = XCDR (tem),
  6099               tem = CONSP (tem) ? XCAR (tem) : Qnil,
  6100               (NILP (tem)
  6101                || EQ (tem, Qleft_margin)
  6102                || EQ (tem, Qright_margin))))
  6103         location = tem;
  6104     }
  6105 
  6106   if (BASE_EQ (location, Qunbound))
  6107     {
  6108       location = Qnil;
  6109       value = spec;
  6110     }
  6111 
  6112   /* After this point, VALUE is the property after any
  6113      margin prefix has been stripped.  It must be a string,
  6114      an image specification, or `(space ...)'.
  6115 
  6116      LOCATION specifies where to display: `left-margin',
  6117      `right-margin' or nil.  */
  6118 
  6119   bool valid_p = (STRINGP (value)
  6120 #ifdef HAVE_WINDOW_SYSTEM
  6121                   || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6122                       && valid_image_p (value))
  6123 #endif /* not HAVE_WINDOW_SYSTEM */
  6124              || (CONSP (value) && EQ (XCAR (value), Qspace))
  6125              || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6126                  && valid_xwidget_spec_p (value)));
  6127 
  6128   if (valid_p && display_replaced == 0)
  6129     {
  6130       int retval = 1;
  6131 
  6132       if (!it)
  6133         {
  6134           /* Callers need to know whether the display spec is any kind
  6135              of `(space ...)' spec that is about to affect text-area
  6136              display.  */
  6137           if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
  6138             retval = 2;
  6139           return retval;
  6140         }
  6141 
  6142       /* Save current settings of IT so that we can restore them
  6143          when we are finished with the glyph property value.  */
  6144       push_it (it, position);
  6145       it->from_overlay = overlay;
  6146       it->from_disp_prop_p = true;
  6147 
  6148       if (NILP (location))
  6149         it->area = TEXT_AREA;
  6150       else if (EQ (location, Qleft_margin))
  6151         it->area = LEFT_MARGIN_AREA;
  6152       else
  6153         it->area = RIGHT_MARGIN_AREA;
  6154 
  6155       if (STRINGP (value))
  6156         {
  6157           it->string = value;
  6158           it->multibyte_p = STRING_MULTIBYTE (it->string);
  6159           it->current.overlay_string_index = -1;
  6160           IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6161           it->end_charpos = it->string_nchars = SCHARS (it->string);
  6162           it->method = GET_FROM_STRING;
  6163           it->stop_charpos = 0;
  6164           it->prev_stop = 0;
  6165           it->base_level_stop = 0;
  6166           it->string_from_display_prop_p = true;
  6167           it->cmp_it.id = -1;
  6168           /* Say that we haven't consumed the characters with
  6169              `display' property yet.  The call to pop_it in
  6170              set_iterator_to_next will clean this up.  */
  6171           if (BUFFERP (object))
  6172             *position = start_pos;
  6173 
  6174           /* Force paragraph direction to be that of the parent
  6175              object.  If the parent object's paragraph direction is
  6176              not yet determined, default to L2R.  */
  6177           if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6178             it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6179           else
  6180             it->paragraph_embedding = L2R;
  6181 
  6182           /* Set up the bidi iterator for this display string.  */
  6183           if (it->bidi_p)
  6184             {
  6185               it->bidi_it.string.lstring = it->string;
  6186               it->bidi_it.string.s = NULL;
  6187               it->bidi_it.string.schars = it->end_charpos;
  6188               it->bidi_it.string.bufpos = bufpos;
  6189               it->bidi_it.string.from_disp_str = true;
  6190               it->bidi_it.string.unibyte = !it->multibyte_p;
  6191               it->bidi_it.w = it->w;
  6192               bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6193             }
  6194         }
  6195       else if (CONSP (value) && EQ (XCAR (value), Qspace))
  6196         {
  6197           it->method = GET_FROM_STRETCH;
  6198           it->object = value;
  6199           *position = it->position = start_pos;
  6200           retval = 1 + (it->area == TEXT_AREA);
  6201         }
  6202       else if (valid_xwidget_spec_p (value))
  6203         {
  6204           it->what = IT_XWIDGET;
  6205           it->method = GET_FROM_XWIDGET;
  6206           it->position = start_pos;
  6207           it->object = NILP (object) ? it->w->contents : object;
  6208           *position = start_pos;
  6209           it->xwidget = lookup_xwidget (value);
  6210         }
  6211 #ifdef HAVE_WINDOW_SYSTEM
  6212       else
  6213         {
  6214           specpdl_ref count = SPECPDL_INDEX ();
  6215 
  6216           it->what = IT_IMAGE;
  6217           /* Don't allow quitting from lookup_image, for when we are
  6218              displaying a non-selected window, and the buffer's point
  6219              was temporarily moved to the window-point.  */
  6220           specbind (Qinhibit_quit, Qt);
  6221           it->image_id = lookup_image (it->f, value, it->face_id);
  6222           unbind_to (count, Qnil);
  6223           it->position = start_pos;
  6224           it->object = NILP (object) ? it->w->contents : object;
  6225           it->method = GET_FROM_IMAGE;
  6226 
  6227           /* Say that we haven't consumed the characters with
  6228              `display' property yet.  The call to pop_it in
  6229              set_iterator_to_next will clean this up.  */
  6230           *position = start_pos;
  6231         }
  6232 #endif /* HAVE_WINDOW_SYSTEM */
  6233 
  6234       return retval;
  6235     }
  6236 
  6237   /* Invalid property or property not supported.  Restore
  6238      POSITION to what it was before.  */
  6239   *position = start_pos;
  6240   return 0;
  6241 }
  6242 
  6243 /* Check if PROP is a display property value whose text should be
  6244    treated as intangible.  OVERLAY is the overlay from which PROP
  6245    came, or nil if it came from a text property.  CHARPOS and BYTEPOS
  6246    specify the buffer position covered by PROP.  */
  6247 
  6248 bool
  6249 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
  6250                            ptrdiff_t charpos, ptrdiff_t bytepos)
  6251 {
  6252   bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
  6253   struct text_pos position;
  6254 
  6255   SET_TEXT_POS (position, charpos, bytepos);
  6256   return (handle_display_spec (NULL, prop, Qnil, overlay,
  6257                                &position, charpos, frame_window_p)
  6258           != 0);
  6259 }
  6260 
  6261 
  6262 /* Return true if PROP is a display sub-property value containing STRING.
  6263 
  6264    Implementation note: this and the following function are really
  6265    special cases of handle_display_spec and
  6266    handle_single_display_spec, and should ideally use the same code.
  6267    Until they do, these two pairs must be consistent and must be
  6268    modified in sync.  */
  6269 
  6270 static bool
  6271 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
  6272 {
  6273   if (EQ (string, prop))
  6274     return true;
  6275 
  6276   /* Skip over `when FORM'.  */
  6277   if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
  6278     {
  6279       prop = XCDR (prop);
  6280       if (!CONSP (prop))
  6281         return false;
  6282       /* Actually, the condition following `when' should be eval'ed,
  6283          like handle_single_display_spec does, and we should return
  6284          false if it evaluates to nil.  However, this function is
  6285          called only when the buffer was already displayed and some
  6286          glyph in the glyph matrix was found to come from a display
  6287          string.  Therefore, the condition was already evaluated, and
  6288          the result was non-nil, otherwise the display string wouldn't
  6289          have been displayed and we would have never been called for
  6290          this property.  Thus, we can skip the evaluation and assume
  6291          its result is non-nil.  */
  6292       prop = XCDR (prop);
  6293     }
  6294 
  6295   if (CONSP (prop))
  6296     /* Skip over `margin LOCATION'.  */
  6297     if (EQ (XCAR (prop), Qmargin))
  6298       {
  6299         prop = XCDR (prop);
  6300         if (!CONSP (prop))
  6301           return false;
  6302 
  6303         prop = XCDR (prop);
  6304         if (!CONSP (prop))
  6305           return false;
  6306       }
  6307 
  6308   return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
  6309 }
  6310 
  6311 
  6312 /* Return true if STRING appears in the `display' property PROP.  */
  6313 
  6314 static bool
  6315 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
  6316 {
  6317   if (CONSP (prop)
  6318       && !EQ (XCAR (prop), Qwhen)
  6319       && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
  6320     {
  6321       /* A list of sub-properties.  */
  6322       while (CONSP (prop))
  6323         {
  6324           if (single_display_spec_string_p (XCAR (prop), string))
  6325             return true;
  6326           prop = XCDR (prop);
  6327         }
  6328     }
  6329   else if (VECTORP (prop))
  6330     {
  6331       /* A vector of sub-properties.  */
  6332       ptrdiff_t i;
  6333       for (i = 0; i < ASIZE (prop); ++i)
  6334         if (single_display_spec_string_p (AREF (prop, i), string))
  6335           return true;
  6336     }
  6337   else
  6338     return single_display_spec_string_p (prop, string);
  6339 
  6340   return false;
  6341 }
  6342 
  6343 /* Look for STRING in overlays and text properties in the current
  6344    buffer, between character positions FROM and TO (excluding TO).
  6345    BACK_P means look back (in this case, TO is supposed to be
  6346    less than FROM).
  6347    Value is the first character position where STRING was found, or
  6348    zero if it wasn't found before hitting TO.
  6349 
  6350    This function may only use code that doesn't eval because it is
  6351    called asynchronously from note_mouse_highlight.  */
  6352 
  6353 static ptrdiff_t
  6354 string_buffer_position_lim (Lisp_Object string,
  6355                             ptrdiff_t from, ptrdiff_t to, bool back_p)
  6356 {
  6357   Lisp_Object limit, prop, pos;
  6358   bool found = false;
  6359 
  6360   pos = make_fixnum (max (from, BEGV));
  6361 
  6362   if (!back_p)  /* looking forward */
  6363     {
  6364       limit = make_fixnum (min (to, ZV));
  6365       while (!found && !EQ (pos, limit))
  6366         {
  6367           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6368           if (!NILP (prop) && display_prop_string_p (prop, string))
  6369             found = true;
  6370           else
  6371             pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
  6372                                                      limit);
  6373         }
  6374     }
  6375   else          /* looking back */
  6376     {
  6377       limit = make_fixnum (max (to, BEGV));
  6378       while (!found && !EQ (pos, limit))
  6379         {
  6380           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6381           if (!NILP (prop) && display_prop_string_p (prop, string))
  6382             found = true;
  6383           else
  6384             pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
  6385                                                          limit);
  6386         }
  6387     }
  6388 
  6389   return found ? XFIXNUM (pos) : 0;
  6390 }
  6391 
  6392 /* Determine which buffer position in current buffer STRING comes from.
  6393    AROUND_CHARPOS is an approximate position where it could come from.
  6394    Value is the buffer position or 0 if it couldn't be determined.
  6395 
  6396    This function is necessary because we don't record buffer positions
  6397    in glyphs generated from strings (to keep struct glyph small).
  6398    This function may only use code that doesn't eval because it is
  6399    called asynchronously from note_mouse_highlight.  */
  6400 
  6401 static ptrdiff_t
  6402 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
  6403 {
  6404   const int MAX_DISTANCE = 1000;
  6405   ptrdiff_t forward_limit = min (around_charpos + MAX_DISTANCE, ZV);
  6406   ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
  6407                                                 forward_limit, false);
  6408 
  6409   if (!found)
  6410     {
  6411       ptrdiff_t backward_limit = max (around_charpos - MAX_DISTANCE, BEGV);
  6412       found = string_buffer_position_lim (string, around_charpos,
  6413                                           backward_limit, true);
  6414     }
  6415   return found;
  6416 }
  6417 
  6418 
  6419 
  6420 /***********************************************************************
  6421                         `composition' property
  6422  ***********************************************************************/
  6423 
  6424 /* Set up iterator IT from `composition' property at its current
  6425    position.  Called from handle_stop.  */
  6426 
  6427 static enum prop_handled
  6428 handle_composition_prop (struct it *it)
  6429 {
  6430   Lisp_Object prop, string;
  6431   ptrdiff_t pos, pos_byte, start, end;
  6432 
  6433   if (STRINGP (it->string))
  6434     {
  6435       unsigned char *s;
  6436 
  6437       pos = IT_STRING_CHARPOS (*it);
  6438       pos_byte = IT_STRING_BYTEPOS (*it);
  6439       string = it->string;
  6440       s = SDATA (string) + pos_byte;
  6441       if (STRING_MULTIBYTE (string))
  6442         it->c = STRING_CHAR (s);
  6443       else
  6444         it->c = *s;
  6445     }
  6446   else
  6447     {
  6448       pos = IT_CHARPOS (*it);
  6449       pos_byte = IT_BYTEPOS (*it);
  6450       string = Qnil;
  6451       it->c = FETCH_CHAR (pos_byte);
  6452     }
  6453 
  6454   /* If there's a valid composition and point is not inside of the
  6455      composition (in the case that the composition is from the current
  6456      buffer), draw a glyph composed from the composition components.  */
  6457   if (find_composition (pos, -1, &start, &end, &prop, string)
  6458       && composition_valid_p (start, end, prop)
  6459       && (STRINGP (it->string) || (PT <= start || PT >= end)))
  6460     {
  6461       if (start < pos)
  6462         /* As we can't handle this situation (perhaps font-lock added
  6463            a new composition), we just return here hoping that next
  6464            redisplay will detect this composition much earlier.  */
  6465         return HANDLED_NORMALLY;
  6466       if (start != pos)
  6467         {
  6468           if (STRINGP (it->string))
  6469             pos_byte = string_char_to_byte (it->string, start);
  6470           else
  6471             pos_byte = CHAR_TO_BYTE (start);
  6472         }
  6473       it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
  6474                                                prop, string);
  6475 
  6476       if (it->cmp_it.id >= 0)
  6477         {
  6478           it->cmp_it.ch = -1;
  6479           it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
  6480           it->cmp_it.nglyphs = -1;
  6481         }
  6482     }
  6483 
  6484   return HANDLED_NORMALLY;
  6485 }
  6486 
  6487 
  6488 
  6489 /***********************************************************************
  6490                            Overlay strings
  6491  ***********************************************************************/
  6492 
  6493 /* The following structure is used to record overlay strings for
  6494    later sorting in load_overlay_strings.  */
  6495 
  6496 struct overlay_entry
  6497 {
  6498   Lisp_Object overlay;
  6499   Lisp_Object string;
  6500   EMACS_INT priority;
  6501   bool after_string_p;
  6502 };
  6503 
  6504 
  6505 /* Set up iterator IT from overlay strings at its current position.
  6506    Called from handle_stop.  */
  6507 
  6508 static enum prop_handled
  6509 handle_overlay_change (struct it *it)
  6510 {
  6511   if (!STRINGP (it->string) && get_overlay_strings (it, 0))
  6512     return HANDLED_RECOMPUTE_PROPS;
  6513   else
  6514     return HANDLED_NORMALLY;
  6515 }
  6516 
  6517 
  6518 /* Set up the next overlay string for delivery by IT, if there is an
  6519    overlay string to deliver.  Called by set_iterator_to_next when the
  6520    end of the current overlay string is reached.  If there are more
  6521    overlay strings to display, IT->string and
  6522    IT->current.overlay_string_index are set appropriately here.
  6523    Otherwise IT->string is set to nil.  */
  6524 
  6525 static void
  6526 next_overlay_string (struct it *it)
  6527 {
  6528   ++it->current.overlay_string_index;
  6529   if (it->current.overlay_string_index == it->n_overlay_strings)
  6530     {
  6531       /* No more overlay strings.  Restore IT's settings to what
  6532          they were before overlay strings were processed, and
  6533          continue to deliver from current_buffer.  */
  6534 
  6535       it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
  6536       pop_it (it);
  6537       eassert (it->sp > 0
  6538                || (NILP (it->string)
  6539                    && it->method == GET_FROM_BUFFER
  6540                    && it->stop_charpos >= BEGV
  6541                    && it->stop_charpos <= it->end_charpos));
  6542       it->current.overlay_string_index = -1;
  6543       it->n_overlay_strings = 0;
  6544       /* If there's an empty display string on the stack, pop the
  6545          stack, to resync the bidi iterator with IT's position.  Such
  6546          empty strings are pushed onto the stack in
  6547          get_overlay_strings_1.  */
  6548       if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
  6549         pop_it (it);
  6550 
  6551       /* Since we've exhausted overlay strings at this buffer
  6552          position, set the flag to ignore overlays until we move to
  6553          another position.  (The flag will be reset in
  6554          next_element_from_buffer.)  But don't do that if the overlay
  6555          strings were loaded at position other than the current one,
  6556          which could happen if we called pop_it above, or if the
  6557          overlay strings were loaded by handle_invisible_prop at the
  6558          beginning of invisible text.  */
  6559       if (it->overlay_strings_charpos == IT_CHARPOS (*it))
  6560         it->ignore_overlay_strings_at_pos_p = true;
  6561 
  6562       /* If we're at the end of the buffer, record that we have
  6563          processed the overlay strings there already, so that
  6564          next_element_from_buffer doesn't try it again.  */
  6565       if (NILP (it->string)
  6566           && IT_CHARPOS (*it) >= it->end_charpos
  6567           && it->overlay_strings_charpos >= it->end_charpos)
  6568         it->overlay_strings_at_end_processed_p = true;
  6569       /* Note: we reset overlay_strings_charpos only here, to make
  6570          sure the just-processed overlays were indeed at EOB.
  6571          Otherwise, overlays on text with invisible text property,
  6572          which are processed with IT's position past the invisible
  6573          text, might fool us into thinking the overlays at EOB were
  6574          already processed (linum-mode can cause this, for
  6575          example).  */
  6576       it->overlay_strings_charpos = -1;
  6577     }
  6578   else
  6579     {
  6580       /* There are more overlay strings to process.  If
  6581          IT->current.overlay_string_index has advanced to a position
  6582          where we must load IT->overlay_strings with more strings, do
  6583          it.  We must load at the IT->overlay_strings_charpos where
  6584          IT->n_overlay_strings was originally computed; when invisible
  6585          text is present, this might not be IT_CHARPOS (Bug#7016).  */
  6586       int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
  6587 
  6588       if (it->current.overlay_string_index && i == 0)
  6589         load_overlay_strings (it, it->overlay_strings_charpos);
  6590 
  6591       /* Initialize IT to deliver display elements from the overlay
  6592          string.  */
  6593       it->string = it->overlay_strings[i];
  6594       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6595       SET_TEXT_POS (it->current.string_pos, 0, 0);
  6596       it->method = GET_FROM_STRING;
  6597       it->stop_charpos = 0;
  6598       it->end_charpos = SCHARS (it->string);
  6599       if (it->cmp_it.stop_pos >= 0)
  6600         it->cmp_it.stop_pos = 0;
  6601       it->prev_stop = 0;
  6602       it->base_level_stop = 0;
  6603 
  6604       /* Set up the bidi iterator for this overlay string.  */
  6605       if (it->bidi_p)
  6606         {
  6607           it->bidi_it.string.lstring = it->string;
  6608           it->bidi_it.string.s = NULL;
  6609           it->bidi_it.string.schars = SCHARS (it->string);
  6610           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  6611           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6612           it->bidi_it.string.unibyte = !it->multibyte_p;
  6613           it->bidi_it.w = it->w;
  6614           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6615         }
  6616     }
  6617 
  6618   CHECK_IT (it);
  6619 }
  6620 
  6621 
  6622 /* Compare two overlay_entry structures E1 and E2.  Used as a
  6623    comparison function for qsort in load_overlay_strings.  Overlay
  6624    strings for the same position are sorted so that
  6625 
  6626    1. All after-strings come in front of before-strings, except
  6627    when they come from the same overlay.
  6628 
  6629    2. Within after-strings, strings are sorted so that overlay strings
  6630    from overlays with higher priorities come first.
  6631 
  6632    2. Within before-strings, strings are sorted so that overlay
  6633    strings from overlays with higher priorities come last.
  6634 
  6635    Value is analogous to strcmp.  */
  6636 
  6637 
  6638 static int
  6639 compare_overlay_entries (const void *e1, const void *e2)
  6640 {
  6641   struct overlay_entry const *entry1 = e1;
  6642   struct overlay_entry const *entry2 = e2;
  6643   int result;
  6644 
  6645   if (entry1->after_string_p != entry2->after_string_p)
  6646     {
  6647       /* Let after-strings appear in front of before-strings if
  6648          they come from different overlays.  */
  6649       if (EQ (entry1->overlay, entry2->overlay))
  6650         result = entry1->after_string_p ? 1 : -1;
  6651       else
  6652         result = entry1->after_string_p ? -1 : 1;
  6653     }
  6654   else if (entry1->priority != entry2->priority)
  6655     {
  6656       if (entry1->after_string_p)
  6657         /* After-strings sorted in order of decreasing priority.  */
  6658         result = entry2->priority < entry1->priority ? -1 : 1;
  6659       else
  6660         /* Before-strings sorted in order of increasing priority.  */
  6661         result = entry1->priority < entry2->priority ? -1 : 1;
  6662     }
  6663   else
  6664     result = 0;
  6665 
  6666   return result;
  6667 }
  6668 
  6669 
  6670 /* Load the vector IT->overlay_strings with overlay strings from IT's
  6671    current buffer position, or from CHARPOS if that is > 0.  Set
  6672    IT->n_overlays to the total number of overlay strings found.
  6673 
  6674    Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
  6675    a time.  On entry into load_overlay_strings,
  6676    IT->current.overlay_string_index gives the number of overlay
  6677    strings that have already been loaded by previous calls to this
  6678    function.
  6679 
  6680    IT->add_overlay_start contains an additional overlay start
  6681    position to consider for taking overlay strings from, if non-zero.
  6682    This position comes into play when the overlay has an `invisible'
  6683    property, and both before and after-strings.  When we've skipped to
  6684    the end of the overlay, because of its `invisible' property, we
  6685    nevertheless want its before-string to appear.
  6686    IT->add_overlay_start will contain the overlay start position
  6687    in this case.
  6688 
  6689    Overlay strings are sorted so that after-string strings come in
  6690    front of before-string strings.  Within before and after-strings,
  6691    strings are sorted by overlay priority.  See also function
  6692    compare_overlay_entries.  */
  6693 
  6694 static void
  6695 load_overlay_strings (struct it *it, ptrdiff_t charpos)
  6696 {
  6697   ptrdiff_t n = 0;
  6698   struct overlay_entry entriesbuf[20];
  6699   ptrdiff_t size = ARRAYELTS (entriesbuf);
  6700   struct overlay_entry *entries = entriesbuf;
  6701   struct itree_node *node;
  6702 
  6703   USE_SAFE_ALLOCA;
  6704 
  6705   if (charpos <= 0)
  6706     charpos = IT_CHARPOS (*it);
  6707 
  6708   /* Append the overlay string STRING of overlay OVERLAY to vector
  6709      `entries' which has size `size' and currently contains `n'
  6710      elements.  AFTER_P means STRING is an after-string of
  6711      OVERLAY.  */
  6712 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P)                 \
  6713   do                                                                    \
  6714     {                                                                   \
  6715       Lisp_Object priority;                                             \
  6716                                                                         \
  6717       if (n == size)                                                    \
  6718         {                                                               \
  6719           struct overlay_entry *old = entries;                          \
  6720           SAFE_NALLOCA (entries, 2, size);                              \
  6721           memcpy (entries, old, size * sizeof *entries);                \
  6722           size *= 2;                                                    \
  6723         }                                                               \
  6724                                                                         \
  6725       entries[n].string = (STRING);                                     \
  6726       entries[n].overlay = (OVERLAY);                                   \
  6727       priority = Foverlay_get ((OVERLAY), Qpriority);                   \
  6728       entries[n].priority = FIXNUMP (priority) ? XFIXNUM (priority) : 0;  \
  6729       entries[n].after_string_p = (AFTER_P);                            \
  6730       ++n;                                                              \
  6731     }                                                                   \
  6732   while (false)
  6733 
  6734 
  6735   /* Process overlays.  */
  6736   ITREE_FOREACH (node, current_buffer->overlays, charpos - 1, charpos + 1, DESCENDING)
  6737     {
  6738       Lisp_Object overlay = node->data;
  6739       eassert (OVERLAYP (overlay));
  6740       ptrdiff_t start = node->begin;
  6741       ptrdiff_t end = node->end;
  6742 
  6743       /* Skip this overlay if it doesn't start or end at IT's current
  6744          position.  */
  6745       if (end != charpos && start != charpos)
  6746         continue;
  6747 
  6748       /* Skip this overlay if it doesn't apply to IT->w.  */
  6749       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  6750       if (WINDOWP (window) && XWINDOW (window) != it->w)
  6751         continue;
  6752 
  6753       /* If the text ``under'' the overlay is invisible, both before-
  6754          and after-strings from this overlay are visible; start and
  6755          end position are indistinguishable.  */
  6756       Lisp_Object invisible = Foverlay_get (overlay, Qinvisible);
  6757       int invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
  6758 
  6759       /* If overlay has a non-empty before-string, record it.  */
  6760       Lisp_Object str;
  6761       if ((start == charpos || (end == charpos && invis != 0))
  6762           && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
  6763           && SCHARS (str))
  6764         RECORD_OVERLAY_STRING (overlay, str, false);
  6765 
  6766       /* If overlay has a non-empty after-string, record it.  */
  6767       if ((end == charpos || (start == charpos && invis != 0))
  6768           && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
  6769           && SCHARS (str))
  6770         RECORD_OVERLAY_STRING (overlay, str, true);
  6771     }
  6772 
  6773 #undef RECORD_OVERLAY_STRING
  6774 
  6775   /* Sort entries.  */
  6776   if (n > 1)
  6777     qsort (entries, n, sizeof *entries, compare_overlay_entries);
  6778 
  6779   /* Record number of overlay strings, and where we computed it.  */
  6780   it->n_overlay_strings = n;
  6781   it->overlay_strings_charpos = charpos;
  6782 
  6783   /* IT->current.overlay_string_index is the number of overlay strings
  6784      that have already been consumed by IT.  Copy some of the
  6785      remaining overlay strings to IT->overlay_strings.  */
  6786   ptrdiff_t j = it->current.overlay_string_index;
  6787   for (ptrdiff_t i = 0; i < OVERLAY_STRING_CHUNK_SIZE && j < n; i++, j++)
  6788     {
  6789       it->overlay_strings[i] = entries[j].string;
  6790       it->string_overlays[i] = entries[j].overlay;
  6791     }
  6792 
  6793   CHECK_IT (it);
  6794   SAFE_FREE ();
  6795 }
  6796 
  6797 
  6798 /* Get the first chunk of overlay strings at IT's current buffer
  6799    position, or at CHARPOS if that is > 0.  Value is true if at
  6800    least one overlay string was found.  */
  6801 
  6802 static bool
  6803 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
  6804 {
  6805   /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
  6806      process.  This fills IT->overlay_strings with strings, and sets
  6807      IT->n_overlay_strings to the total number of strings to process.
  6808      IT->pos.overlay_string_index has to be set temporarily to zero
  6809      because load_overlay_strings needs this; it must be set to -1
  6810      when no overlay strings are found because a zero value would
  6811      indicate a position in the first overlay string.  */
  6812   it->current.overlay_string_index = 0;
  6813   load_overlay_strings (it, charpos);
  6814 
  6815   /* If we found overlay strings, set up IT to deliver display
  6816      elements from the first one.  Otherwise set up IT to deliver
  6817      from current_buffer.  */
  6818   if (it->n_overlay_strings)
  6819     {
  6820       /* Make sure we know settings in current_buffer, so that we can
  6821          restore meaningful values when we're done with the overlay
  6822          strings.  */
  6823       if (compute_stop_p)
  6824         compute_stop_pos (it);
  6825       eassert (it->face_id >= 0);
  6826 
  6827       /* Save IT's settings.  They are restored after all overlay
  6828          strings have been processed.  */
  6829       eassert (!compute_stop_p || it->sp == 0);
  6830 
  6831       /* When called from handle_stop, there might be an empty display
  6832          string loaded.  In that case, don't bother saving it.  But
  6833          don't use this optimization with the bidi iterator, since we
  6834          need the corresponding pop_it call to resync the bidi
  6835          iterator's position with IT's position, after we are done
  6836          with the overlay strings.  (The corresponding call to pop_it
  6837          in case of an empty display string is in
  6838          next_overlay_string.)  */
  6839       if (!(!it->bidi_p
  6840             && STRINGP (it->string) && !SCHARS (it->string)))
  6841         push_it (it, NULL);
  6842 
  6843       /* Set up IT to deliver display elements from the first overlay
  6844          string.  */
  6845       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6846       it->string = it->overlay_strings[0];
  6847       it->from_overlay = Qnil;
  6848       it->stop_charpos = 0;
  6849       eassert (STRINGP (it->string));
  6850       it->end_charpos = SCHARS (it->string);
  6851       it->prev_stop = 0;
  6852       it->base_level_stop = 0;
  6853       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6854       it->method = GET_FROM_STRING;
  6855       it->from_disp_prop_p = 0;
  6856       it->cmp_it.id = -1;
  6857 
  6858       /* Force paragraph direction to be that of the parent
  6859          buffer.  */
  6860       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6861         it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6862       else
  6863         it->paragraph_embedding = L2R;
  6864 
  6865       /* Set up the bidi iterator for this overlay string.  */
  6866       if (it->bidi_p)
  6867         {
  6868           ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
  6869 
  6870           it->bidi_it.string.lstring = it->string;
  6871           it->bidi_it.string.s = NULL;
  6872           it->bidi_it.string.schars = SCHARS (it->string);
  6873           it->bidi_it.string.bufpos = pos;
  6874           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6875           it->bidi_it.string.unibyte = !it->multibyte_p;
  6876           it->bidi_it.w = it->w;
  6877           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6878         }
  6879       return true;
  6880     }
  6881 
  6882   it->current.overlay_string_index = -1;
  6883   return false;
  6884 }
  6885 
  6886 static bool
  6887 get_overlay_strings (struct it *it, ptrdiff_t charpos)
  6888 {
  6889   it->string = Qnil;
  6890   it->method = GET_FROM_BUFFER;
  6891 
  6892   get_overlay_strings_1 (it, charpos, true);
  6893 
  6894   CHECK_IT (it);
  6895 
  6896   /* Value is true if we found at least one overlay string.  */
  6897   return STRINGP (it->string);
  6898 }
  6899 
  6900 
  6901 
  6902 /***********************************************************************
  6903                       Saving and restoring state
  6904  ***********************************************************************/
  6905 
  6906 /* Save current settings of IT on IT->stack.  Called, for example,
  6907    before setting up IT for an overlay string, to be able to restore
  6908    IT's settings to what they were after the overlay string has been
  6909    processed.  If POSITION is non-NULL, it is the position to save on
  6910    the stack instead of IT->position.  */
  6911 
  6912 static void
  6913 push_it (struct it *it, struct text_pos *position)
  6914 {
  6915   struct iterator_stack_entry *p;
  6916 
  6917   eassert (it->sp < IT_STACK_SIZE);
  6918   p = it->stack + it->sp;
  6919 
  6920   p->stop_charpos = it->stop_charpos;
  6921   p->prev_stop = it->prev_stop;
  6922   p->base_level_stop = it->base_level_stop;
  6923   p->cmp_it = it->cmp_it;
  6924   eassert (it->face_id >= 0);
  6925   p->face_id = it->face_id;
  6926   p->string = it->string;
  6927   p->method = it->method;
  6928   p->from_overlay = it->from_overlay;
  6929   switch (p->method)
  6930     {
  6931     case GET_FROM_IMAGE:
  6932       p->u.image.object = it->object;
  6933       p->u.image.image_id = it->image_id;
  6934       p->u.image.slice = it->slice;
  6935       break;
  6936     case GET_FROM_STRETCH:
  6937       p->u.stretch.object = it->object;
  6938       break;
  6939     case GET_FROM_XWIDGET:
  6940       p->u.xwidget.object = it->object;
  6941       break;
  6942     case GET_FROM_BUFFER:
  6943     case GET_FROM_DISPLAY_VECTOR:
  6944     case GET_FROM_STRING:
  6945     case GET_FROM_C_STRING:
  6946       break;
  6947     default:
  6948       emacs_abort ();
  6949     }
  6950   p->position = position ? *position : it->position;
  6951   p->current = it->current;
  6952   p->end_charpos = it->end_charpos;
  6953   p->string_nchars = it->string_nchars;
  6954   p->area = it->area;
  6955   p->multibyte_p = it->multibyte_p;
  6956   p->avoid_cursor_p = it->avoid_cursor_p;
  6957   p->space_width = it->space_width;
  6958   p->font_height = it->font_height;
  6959   p->voffset = it->voffset;
  6960   p->string_from_display_prop_p = it->string_from_display_prop_p;
  6961   p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
  6962   p->display_ellipsis_p = false;
  6963   p->line_wrap = it->line_wrap;
  6964   p->bidi_p = it->bidi_p;
  6965   p->paragraph_embedding = it->paragraph_embedding;
  6966   p->from_disp_prop_p = it->from_disp_prop_p;
  6967   ++it->sp;
  6968 
  6969   /* Save the state of the bidi iterator as well. */
  6970   if (it->bidi_p)
  6971     bidi_push_it (&it->bidi_it);
  6972 }
  6973 
  6974 static void
  6975 iterate_out_of_display_property (struct it *it)
  6976 {
  6977   bool buffer_p = !STRINGP (it->string);
  6978   ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
  6979   ptrdiff_t bob = (buffer_p ? BEGV : 0);
  6980 
  6981   eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
  6982 
  6983   /* Maybe initialize paragraph direction.  If we are at the beginning
  6984      of a new paragraph, next_element_from_buffer may not have a
  6985      chance to do that.  */
  6986   if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
  6987     bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  6988   /* prev_stop can be zero, so check against BEGV as well.  */
  6989   while (it->bidi_it.charpos >= bob
  6990          && it->prev_stop <= it->bidi_it.charpos
  6991          && it->bidi_it.charpos < CHARPOS (it->position)
  6992          && it->bidi_it.charpos < eob)
  6993     bidi_move_to_visually_next (&it->bidi_it);
  6994   /* Record the stop_pos we just crossed, for when we cross it
  6995      back, maybe.  */
  6996   if (it->bidi_it.charpos > CHARPOS (it->position))
  6997     it->prev_stop = CHARPOS (it->position);
  6998   /* If we ended up not where pop_it put us, resync IT's
  6999      positional members with the bidi iterator. */
  7000   if (it->bidi_it.charpos != CHARPOS (it->position))
  7001     SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
  7002   if (buffer_p)
  7003     it->current.pos = it->position;
  7004   else
  7005     it->current.string_pos = it->position;
  7006 }
  7007 
  7008 /* Restore the IT->face_box_p flag, since it could have been
  7009    overwritten by the face of the object that we just finished
  7010    displaying.  Also, set the IT->start_of_box_run_p flag if the
  7011    change in faces requires that.  */
  7012 static void
  7013 restore_face_box_flags (struct it *it, int prev_face_id)
  7014 {
  7015   struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  7016 
  7017   if (face)
  7018     {
  7019       struct face *prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  7020 
  7021       if (!(it->start_of_box_run_p && prev_face && prev_face->box))
  7022         it->start_of_box_run_p = (face->box != FACE_NO_BOX
  7023                                   && (prev_face == NULL
  7024                                       || prev_face->box == FACE_NO_BOX));
  7025       it->face_box_p = face->box != FACE_NO_BOX;
  7026     }
  7027 }
  7028 
  7029 /* Restore IT's settings from IT->stack.  Called, for example, when no
  7030    more overlay strings must be processed, and we return to delivering
  7031    display elements from a buffer, or when the end of a string from a
  7032    `display' property is reached and we return to delivering display
  7033    elements from an overlay string, or from a buffer.  */
  7034 
  7035 static void
  7036 pop_it (struct it *it)
  7037 {
  7038   struct iterator_stack_entry *p;
  7039   bool from_display_prop = it->from_disp_prop_p;
  7040   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  7041   int prev_face_id = it->face_id;
  7042 
  7043   eassert (it->sp > 0);
  7044   --it->sp;
  7045   p = it->stack + it->sp;
  7046   it->stop_charpos = p->stop_charpos;
  7047   it->prev_stop = p->prev_stop;
  7048   it->base_level_stop = p->base_level_stop;
  7049   it->cmp_it = p->cmp_it;
  7050   it->face_id = p->face_id;
  7051   it->current = p->current;
  7052   it->position = p->position;
  7053   it->string = p->string;
  7054   it->from_overlay = p->from_overlay;
  7055   if (NILP (it->string))
  7056     SET_TEXT_POS (it->current.string_pos, -1, -1);
  7057   it->method = p->method;
  7058   switch (it->method)
  7059     {
  7060     case GET_FROM_IMAGE:
  7061       it->image_id = p->u.image.image_id;
  7062       it->object = p->u.image.object;
  7063       it->slice = p->u.image.slice;
  7064       break;
  7065     case GET_FROM_XWIDGET:
  7066       it->object = p->u.xwidget.object;
  7067       break;
  7068     case GET_FROM_STRETCH:
  7069       it->object = p->u.stretch.object;
  7070       break;
  7071     case GET_FROM_BUFFER:
  7072       {
  7073         restore_face_box_flags (it, prev_face_id);
  7074         it->object = it->w->contents;
  7075       }
  7076       break;
  7077     case GET_FROM_STRING:
  7078       {
  7079         restore_face_box_flags (it, prev_face_id);
  7080         it->object = it->string;
  7081       }
  7082       break;
  7083     case GET_FROM_DISPLAY_VECTOR:
  7084       if (it->s)
  7085         it->method = GET_FROM_C_STRING;
  7086       else if (STRINGP (it->string))
  7087         it->method = GET_FROM_STRING;
  7088       else
  7089         {
  7090           it->method = GET_FROM_BUFFER;
  7091           it->object = it->w->contents;
  7092         }
  7093       break;
  7094     case GET_FROM_C_STRING:
  7095       break;
  7096     default:
  7097       emacs_abort ();
  7098     }
  7099   it->end_charpos = p->end_charpos;
  7100   it->string_nchars = p->string_nchars;
  7101   it->area = p->area;
  7102   it->multibyte_p = p->multibyte_p;
  7103   it->avoid_cursor_p = p->avoid_cursor_p;
  7104   it->space_width = p->space_width;
  7105   it->font_height = p->font_height;
  7106   it->voffset = p->voffset;
  7107   it->string_from_display_prop_p = p->string_from_display_prop_p;
  7108   it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
  7109   it->line_wrap = p->line_wrap;
  7110   it->bidi_p = p->bidi_p;
  7111   it->paragraph_embedding = p->paragraph_embedding;
  7112   it->from_disp_prop_p = p->from_disp_prop_p;
  7113   if (it->bidi_p)
  7114     {
  7115       bidi_pop_it (&it->bidi_it);
  7116       /* Bidi-iterate until we get out of the portion of text, if any,
  7117          covered by a `display' text property or by an overlay with
  7118          `display' property.  (We cannot just jump there, because the
  7119          internal coherency of the bidi iterator state can not be
  7120          preserved across such jumps.)  We also must determine the
  7121          paragraph base direction if the overlay we just processed is
  7122          at the beginning of a new paragraph.  */
  7123       if (from_display_prop
  7124           && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
  7125         iterate_out_of_display_property (it);
  7126 
  7127       eassert ((BUFFERP (it->object)
  7128                 && IT_CHARPOS (*it) == it->bidi_it.charpos
  7129                 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
  7130                || (STRINGP (it->object)
  7131                    && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  7132                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
  7133                || (CONSP (it->object) && it->method == GET_FROM_STRETCH)
  7134                /* We could be in the middle of handling a list or a
  7135                   vector of several 'display' properties, in which
  7136                   case we should only verify the above conditions when
  7137                   we pop the iterator stack the last time, because
  7138                   higher stack levels cannot "iterate out of the
  7139                   display property".  */
  7140                || it->sp > 0);
  7141     }
  7142   /* If we move the iterator over text covered by a display property
  7143      to a new buffer position, any info about previously seen overlays
  7144      is no longer valid.  */
  7145   if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
  7146     it->ignore_overlay_strings_at_pos_p = false;
  7147 }
  7148 
  7149 
  7150 
  7151 /***********************************************************************
  7152                           Moving over lines
  7153  ***********************************************************************/
  7154 
  7155 /* Set IT's current position to the previous line start.  */
  7156 
  7157 static void
  7158 back_to_previous_line_start (struct it *it)
  7159 {
  7160   ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
  7161 
  7162   dec_both (&cp, &bp);
  7163   SET_WITH_NARROWED_BEGV (it, IT_CHARPOS (*it),
  7164                           find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it)),
  7165                           get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
  7166 }
  7167 
  7168 /* Find in the current buffer the first display or overlay string
  7169    between STARTPOS and ENDPOS that includes embedded newlines.
  7170    Consider only overlays that apply to window W.
  7171    Value is non-zero if such a display/overlay string is found.  */
  7172 static bool
  7173 strings_with_newlines (ptrdiff_t startpos, ptrdiff_t endpos, struct window *w)
  7174 {
  7175   struct itree_node *node;
  7176   /* Process overlays.  */
  7177   ITREE_FOREACH (node, current_buffer->overlays, startpos, endpos, DESCENDING)
  7178     {
  7179       Lisp_Object overlay = node->data;
  7180       eassert (OVERLAYP (overlay));
  7181 
  7182       /* Skip this overlay if it doesn't apply to our window.  */
  7183       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  7184       if (WINDOWP (window) && XWINDOW (window) != w)
  7185         continue;
  7186 
  7187       ptrdiff_t ostart = node->begin;
  7188       ptrdiff_t oend = node->end;
  7189 
  7190       /* Skip overlays that don't overlap the range.  */
  7191       if (!((startpos < oend && ostart < endpos)
  7192             || (ostart == oend
  7193                 && (startpos == oend || (endpos == ZV && oend == endpos)))))
  7194         continue;
  7195 
  7196       Lisp_Object str;
  7197       str = Foverlay_get (overlay, Qbefore_string);
  7198       if (STRINGP (str) && SCHARS (str)
  7199           && memchr (SDATA (str), '\n', SBYTES (str)))
  7200         return true;
  7201       str = Foverlay_get (overlay, Qafter_string);
  7202       if (STRINGP (str) && SCHARS (str)
  7203           && memchr (SDATA (str), '\n', SBYTES (str)))
  7204         return true;
  7205     }
  7206 
  7207   /* Check for 'display' properties whose values include strings.  */
  7208   Lisp_Object cpos = make_fixnum (startpos);
  7209   Lisp_Object limpos = make_fixnum (endpos);
  7210 
  7211   while ((cpos = Fnext_single_property_change (cpos, Qdisplay, Qnil, limpos),
  7212           !(NILP (cpos) || XFIXNAT (cpos) >= endpos)))
  7213     {
  7214       Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  7215       Lisp_Object string = string_from_display_spec (spec);
  7216       if (STRINGP (string)
  7217           && memchr (SDATA (string), '\n', SBYTES (string)))
  7218         return true;
  7219     }
  7220 
  7221   return false;
  7222 }
  7223 
  7224 
  7225 /* Move IT to the next line start.
  7226 
  7227    Value is true if a newline was found.  Set *SKIPPED_P to true if
  7228    we skipped over part of the text (as opposed to moving the iterator
  7229    continuously over the text).  Otherwise, don't change the value
  7230    of *SKIPPED_P.
  7231 
  7232    If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
  7233    iterator on the newline, if it was found.
  7234 
  7235    Newlines may come from buffer text, overlay strings, or strings
  7236    displayed via the `display' property.  That's the reason we can't
  7237    simply use find_newline_no_quit.
  7238 
  7239    Note that this function may not skip over invisible text that is so
  7240    because of text properties and immediately follows a newline.  If
  7241    it would, function reseat_at_next_visible_line_start, when called
  7242    from set_iterator_to_next, would effectively make invisible
  7243    characters following a newline part of the wrong glyph row, which
  7244    leads to wrong cursor motion.  */
  7245 
  7246 static bool
  7247 forward_to_next_line_start (struct it *it, bool *skipped_p,
  7248                             struct bidi_it *bidi_it_prev)
  7249 {
  7250   ptrdiff_t old_selective;
  7251   bool newline_found_p = false;
  7252   int n;
  7253   const int MAX_NEWLINE_DISTANCE = 500;
  7254 
  7255   /* If already on a newline, just consume it to avoid unintended
  7256      skipping over invisible text below.  */
  7257   if (it->what == IT_CHARACTER
  7258       && it->c == '\n'
  7259       && CHARPOS (it->position) == IT_CHARPOS (*it))
  7260     {
  7261       if (it->bidi_p && bidi_it_prev)
  7262         *bidi_it_prev = it->bidi_it;
  7263       set_iterator_to_next (it, false);
  7264       it->c = 0;
  7265       return true;
  7266     }
  7267 
  7268   /* Don't handle selective display in the following.  It's (a)
  7269      unnecessary because it's done by the caller, and (b) leads to an
  7270      infinite recursion because next_element_from_ellipsis indirectly
  7271      calls this function.  */
  7272   old_selective = it->selective;
  7273   it->selective = 0;
  7274 
  7275   /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
  7276      from buffer text, or till the end of the string if iterating a
  7277      string.  */
  7278   for (n = 0;
  7279        !newline_found_p && n < MAX_NEWLINE_DISTANCE;
  7280        n += !STRINGP (it->string))
  7281     {
  7282       if (!get_next_display_element (it))
  7283         return false;
  7284       newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
  7285       if (newline_found_p && it->bidi_p && bidi_it_prev)
  7286         *bidi_it_prev = it->bidi_it;
  7287       set_iterator_to_next (it, false);
  7288     }
  7289 
  7290   /* If we didn't find a newline near enough, see if we can use a
  7291      short-cut.  */
  7292   if (!newline_found_p)
  7293     {
  7294       ptrdiff_t bytepos, start = IT_CHARPOS (*it);
  7295       ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
  7296                                               1, &bytepos);
  7297       eassert (!STRINGP (it->string));
  7298 
  7299       /* it->stop_charpos >= limit means we already know there's no
  7300          stop position up until the newline at LIMIT, so there's no
  7301          need for any further checks.  */
  7302       bool no_strings_with_newlines = it->stop_charpos >= limit;
  7303 
  7304       if (!no_strings_with_newlines)
  7305         {
  7306           if (!(current_buffer->long_line_optimizations_p
  7307                 && it->line_wrap == TRUNCATE))
  7308             {
  7309               /* Quick-and-dirty check: if there isn't any `display'
  7310                  property in sight, and no overlays, we're done.  */
  7311               Lisp_Object pos =
  7312                 Fnext_single_property_change (make_fixnum (start),
  7313                                               Qdisplay, Qnil,
  7314                                               make_fixnum (limit));
  7315               no_strings_with_newlines =
  7316                 (NILP (pos) || XFIXNAT (pos) == limit) /* no 'display' props */
  7317                 && next_overlay_change (start) == ZV;  /* no overlays */
  7318             }
  7319           else
  7320             {
  7321               /* For buffers with very long and truncated lines we try
  7322                  harder, because it's worth our while to spend some
  7323                  time looking into the overlays and 'display' properties
  7324                  if we can then avoid iterating through all of them.  */
  7325               no_strings_with_newlines =
  7326                 !strings_with_newlines (start, limit, it->w);
  7327             }
  7328         }
  7329 
  7330       /* If there's no display or overlay strings with embedded
  7331          newlines until the position of the newline in buffer text, we
  7332          can just use that position.  */
  7333       if (no_strings_with_newlines)
  7334         {
  7335           if (!it->bidi_p || !bidi_it_prev)
  7336             {
  7337               /* The optimal case: just jump there.  */
  7338               IT_CHARPOS (*it) = limit;
  7339               IT_BYTEPOS (*it) = bytepos;
  7340             }
  7341           else
  7342             {
  7343               /* The less optimal case: need to bidi-walk there, but
  7344                  this is still cheaper that the full iteration using
  7345                  get_next_display_element and set_iterator_to_next.  */
  7346               struct bidi_it bprev;
  7347 
  7348               /* Help bidi.c avoid expensive searches for display
  7349                  properties and overlays, by telling it that there are
  7350                  none up to `limit'.  */
  7351               if (it->bidi_it.disp_pos < limit)
  7352                 {
  7353                   it->bidi_it.disp_pos = limit;
  7354                   it->bidi_it.disp_prop = 0;
  7355                 }
  7356               do {
  7357                 bprev = it->bidi_it;
  7358                 bidi_move_to_visually_next (&it->bidi_it);
  7359               } while (it->bidi_it.charpos != limit);
  7360               IT_CHARPOS (*it) = limit;
  7361               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7362               if (bidi_it_prev)
  7363                 *bidi_it_prev = bprev;
  7364             }
  7365           *skipped_p = newline_found_p = true;
  7366         }
  7367       else
  7368         {
  7369           /* The slow case.  */
  7370           while (!newline_found_p)
  7371             {
  7372               if (!get_next_display_element (it))
  7373                 break;
  7374               newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
  7375               if (newline_found_p && it->bidi_p && bidi_it_prev)
  7376                 *bidi_it_prev = it->bidi_it;
  7377               set_iterator_to_next (it, false);
  7378             }
  7379         }
  7380     }
  7381 
  7382   it->selective = old_selective;
  7383   return newline_found_p;
  7384 }
  7385 
  7386 
  7387 /* Set IT's current position to the previous visible line start.  Skip
  7388    invisible text that is so either due to text properties or due to
  7389    selective display.  Caution: this does not change IT->current_x and
  7390    IT->hpos.  */
  7391 
  7392 static void
  7393 back_to_previous_visible_line_start (struct it *it)
  7394 {
  7395   while (IT_CHARPOS (*it) > BEGV)
  7396     {
  7397       back_to_previous_line_start (it);
  7398 
  7399       if (IT_CHARPOS (*it) <= BEGV)
  7400         break;
  7401 
  7402       /* If selective > 0, then lines indented more than its value are
  7403          invisible.  */
  7404       if (it->selective > 0
  7405           && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7406                                 it->selective))
  7407         continue;
  7408 
  7409       /* Check the newline before point for invisibility.  */
  7410       {
  7411         Lisp_Object prop;
  7412         prop = Fget_char_property (make_fixnum (IT_CHARPOS (*it) - 1),
  7413                                    Qinvisible, it->window);
  7414         if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
  7415           continue;
  7416       }
  7417 
  7418       if (IT_CHARPOS (*it) <= BEGV)
  7419         break;
  7420 
  7421       {
  7422         struct it it2;
  7423         void *it2data = NULL;
  7424         ptrdiff_t pos;
  7425         ptrdiff_t beg, end;
  7426         Lisp_Object val, overlay;
  7427 
  7428         SAVE_IT (it2, *it, it2data);
  7429 
  7430         /* If newline is part of a composition, continue from start of composition */
  7431         if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
  7432             && beg < IT_CHARPOS (*it))
  7433           goto replaced;
  7434 
  7435         /* If newline is replaced by a display property, find start of overlay
  7436            or interval and continue search from that point.  */
  7437         pos = --IT_CHARPOS (it2);
  7438         --IT_BYTEPOS (it2);
  7439         it2.sp = 0;
  7440         bidi_unshelve_cache (NULL, false);
  7441         it2.string_from_display_prop_p = false;
  7442         it2.from_disp_prop_p = false;
  7443         if (handle_display_prop (&it2) == HANDLED_RETURN
  7444             && !NILP (val = get_char_property_and_overlay
  7445                       (make_fixnum (pos), Qdisplay, Qnil, &overlay))
  7446             && (OVERLAYP (overlay)
  7447                 ? (beg = OVERLAY_START (overlay))
  7448                 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
  7449           {
  7450             RESTORE_IT (it, it, it2data);
  7451             goto replaced;
  7452           }
  7453 
  7454         /* Newline is not replaced by anything -- so we are done.  */
  7455         RESTORE_IT (it, it, it2data);
  7456         break;
  7457 
  7458       replaced:
  7459         if (beg < BEGV)
  7460           beg = BEGV;
  7461         IT_CHARPOS (*it) = beg;
  7462         IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
  7463       }
  7464     }
  7465 
  7466   it->continuation_lines_width = 0;
  7467 
  7468   eassert (IT_CHARPOS (*it) >= BEGV);
  7469   eassert (it->medium_narrowing_begv > 0 /* long-line optimizations: all bets off */
  7470            || IT_CHARPOS (*it) == BEGV
  7471            || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7472   CHECK_IT (it);
  7473 }
  7474 
  7475 
  7476 /* Reseat iterator IT at the previous visible line start.  Skip
  7477    invisible text that is so either due to text properties or due to
  7478    selective display.  At the end, update IT's overlay information,
  7479    face information etc.  */
  7480 
  7481 void
  7482 reseat_at_previous_visible_line_start (struct it *it)
  7483 {
  7484   back_to_previous_visible_line_start (it);
  7485   reseat (it, it->current.pos, true);
  7486   CHECK_IT (it);
  7487 }
  7488 
  7489 
  7490 /* Reseat iterator IT on the next visible line start in the current
  7491    buffer.  ON_NEWLINE_P means position IT on the newline
  7492    preceding the line start.  Skip over invisible text that is so
  7493    because of selective display.  Compute faces, overlays etc at the
  7494    new position.  Note that this function does not skip over text that
  7495    is invisible because of text properties.  */
  7496 
  7497 static void
  7498 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
  7499 {
  7500   bool skipped_p = false;
  7501   struct bidi_it bidi_it_prev;
  7502   bool newline_found_p
  7503     = forward_to_next_line_start (it, &skipped_p,
  7504                                   on_newline_p ? &bidi_it_prev : NULL);
  7505 
  7506   /* Skip over lines that are invisible because they are indented
  7507      more than the value of IT->selective.  */
  7508   if (it->selective > 0)
  7509     while (IT_CHARPOS (*it) < ZV
  7510            && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7511                                  it->selective))
  7512       {
  7513         eassert (IT_BYTEPOS (*it) == BEGV
  7514                  || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7515         newline_found_p =
  7516           forward_to_next_line_start (it, &skipped_p,
  7517                                       on_newline_p ? &bidi_it_prev : NULL);
  7518       }
  7519 
  7520   /* Position on the newline if that's what's requested.  */
  7521   if (on_newline_p && newline_found_p)
  7522     {
  7523       if (STRINGP (it->string))
  7524         {
  7525           if (IT_STRING_CHARPOS (*it) > 0)
  7526             {
  7527               if (!it->bidi_p)
  7528                 {
  7529                   --IT_STRING_CHARPOS (*it);
  7530                   --IT_STRING_BYTEPOS (*it);
  7531                 }
  7532               else
  7533                 {
  7534                   /* We need to restore the bidi iterator to the state
  7535                      it had on the newline, and resync the IT's
  7536                      position with that.  */
  7537                   it->bidi_it = bidi_it_prev;
  7538                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  7539                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  7540                 }
  7541             }
  7542         }
  7543       else if (IT_CHARPOS (*it) > BEGV)
  7544         {
  7545           if (!it->bidi_p)
  7546             {
  7547               --IT_CHARPOS (*it);
  7548               --IT_BYTEPOS (*it);
  7549             }
  7550           else
  7551             {
  7552               /* We need to restore the bidi iterator to the state it
  7553                  had on the newline and resync IT with that.  */
  7554               it->bidi_it = bidi_it_prev;
  7555               IT_CHARPOS (*it) = it->bidi_it.charpos;
  7556               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7557             }
  7558           reseat (it, it->current.pos, false);
  7559         }
  7560     }
  7561   else if (skipped_p)
  7562     reseat (it, it->current.pos, false);
  7563 
  7564   CHECK_IT (it);
  7565 }
  7566 
  7567 
  7568 
  7569 /***********************************************************************
  7570                    Changing an iterator's position
  7571 ***********************************************************************/
  7572 
  7573 /* Change IT's current position to POS in current_buffer.
  7574    If FORCE_P, always check for text properties at the new position.
  7575    Otherwise, text properties are only looked up if POS >=
  7576    IT->check_charpos of a property.  */
  7577 
  7578 static void
  7579 reseat (struct it *it, struct text_pos pos, bool force_p)
  7580 {
  7581   ptrdiff_t original_pos = IT_CHARPOS (*it);
  7582 
  7583   reseat_1 (it, pos, false);
  7584 
  7585   if (current_buffer->long_line_optimizations_p)
  7586     {
  7587       if (!it->medium_narrowing_begv)
  7588         {
  7589           it->medium_narrowing_begv
  7590             = get_medium_narrowing_begv (it->w, window_point (it->w));
  7591           it->medium_narrowing_zv
  7592             = get_medium_narrowing_zv (it->w, window_point (it->w));
  7593           it->large_narrowing_begv
  7594             = get_large_narrowing_begv (window_point (it->w));
  7595           it->large_narrowing_zv
  7596             = get_large_narrowing_zv (window_point (it->w));
  7597         }
  7598       else if ((pos.charpos < it->medium_narrowing_begv
  7599                 || pos.charpos > it->medium_narrowing_zv)
  7600                 && (!redisplaying_p || it->line_wrap == TRUNCATE))
  7601         {
  7602           it->medium_narrowing_begv
  7603             = get_medium_narrowing_begv (it->w, pos.charpos);
  7604           it->medium_narrowing_zv
  7605             = get_medium_narrowing_zv (it->w, pos.charpos);
  7606           it->large_narrowing_begv
  7607             = get_large_narrowing_begv (window_point (it->w));
  7608           it->large_narrowing_zv
  7609             = get_large_narrowing_zv (window_point (it->w));
  7610         }
  7611     }
  7612 
  7613   /* Determine where to check text properties.  Avoid doing it
  7614      where possible because text property lookup is very expensive.  */
  7615   if (force_p
  7616       || CHARPOS (pos) > it->stop_charpos
  7617       || CHARPOS (pos) < original_pos)
  7618     {
  7619       if (it->bidi_p)
  7620         {
  7621           /* For bidi iteration, we need to prime prev_stop and
  7622              base_level_stop with our best estimations.  */
  7623           /* Implementation note: Of course, POS is not necessarily a
  7624              stop position, so assigning prev_pos to it is a lie; we
  7625              should have called compute_stop_backwards.  However, if
  7626              the current buffer does not include any R2L characters,
  7627              that call would be a waste of cycles, because the
  7628              iterator will never move back, and thus never cross this
  7629              "fake" stop position.  So we delay that backward search
  7630              until the time we really need it, in next_element_from_buffer.  */
  7631           if (CHARPOS (pos) != it->prev_stop)
  7632             it->prev_stop = CHARPOS (pos);
  7633           if (CHARPOS (pos) < it->base_level_stop)
  7634             it->base_level_stop = 0; /* meaning it's unknown */
  7635           handle_stop (it);
  7636         }
  7637       else
  7638         {
  7639           handle_stop (it);
  7640           it->prev_stop = it->base_level_stop = 0;
  7641         }
  7642 
  7643     }
  7644 
  7645   CHECK_IT (it);
  7646 }
  7647 
  7648 
  7649 /* Change IT's buffer position to POS.  SET_STOP_P means set
  7650    IT->stop_pos to POS, also.  */
  7651 
  7652 static void
  7653 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
  7654 {
  7655   /* Don't call this function when scanning a C string.  */
  7656   eassert (it->s == NULL);
  7657 
  7658   /* POS must be a reasonable value.  */
  7659   eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
  7660 
  7661   it->current.pos = it->position = pos;
  7662   it->end_charpos = ZV;
  7663   it->dpvec = NULL;
  7664   it->current.dpvec_index = -1;
  7665   it->current.overlay_string_index = -1;
  7666   IT_STRING_CHARPOS (*it) = -1;
  7667   IT_STRING_BYTEPOS (*it) = -1;
  7668   it->string = Qnil;
  7669   it->method = GET_FROM_BUFFER;
  7670   it->object = it->w->contents;
  7671   it->area = TEXT_AREA;
  7672   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  7673   it->sp = 0;
  7674   it->string_from_display_prop_p = false;
  7675   it->string_from_prefix_prop_p = false;
  7676 
  7677   it->from_disp_prop_p = false;
  7678   it->face_before_selective_p = false;
  7679   if (it->bidi_p)
  7680     {
  7681       bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7682                     &it->bidi_it);
  7683       bidi_unshelve_cache (NULL, false);
  7684       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7685       it->bidi_it.string.s = NULL;
  7686       it->bidi_it.string.lstring = Qnil;
  7687       it->bidi_it.string.bufpos = 0;
  7688       it->bidi_it.string.from_disp_str = false;
  7689       it->bidi_it.string.unibyte = false;
  7690       it->bidi_it.w = it->w;
  7691     }
  7692 
  7693   if (set_stop_p)
  7694     {
  7695       it->stop_charpos = CHARPOS (pos);
  7696       it->base_level_stop = CHARPOS (pos);
  7697     }
  7698   /* This make the information stored in it->cmp_it invalidate.  */
  7699   it->cmp_it.id = -1;
  7700   it->min_width_property = Qnil;
  7701 }
  7702 
  7703 
  7704 /* Set up IT for displaying a string, starting at CHARPOS in window W.
  7705    If S is non-null, it is a C string to iterate over.  Otherwise,
  7706    STRING gives a Lisp string to iterate over.
  7707 
  7708    If PRECISION > 0, don't return more then PRECISION number of
  7709    characters from the string.
  7710 
  7711    If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
  7712    characters have been returned.  FIELD_WIDTH < 0 means an infinite
  7713    field width.
  7714 
  7715    MULTIBYTE = 0 means disable processing of multibyte characters,
  7716    MULTIBYTE > 0 means enable it,
  7717    MULTIBYTE < 0 means use IT->multibyte_p.
  7718 
  7719    IT must be initialized via a prior call to init_iterator before
  7720    calling this function.  */
  7721 
  7722 static void
  7723 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
  7724                   ptrdiff_t charpos, ptrdiff_t precision, int field_width,
  7725                   int multibyte)
  7726 {
  7727   /* No text property checks performed by default, but see below.  */
  7728   it->stop_charpos = -1;
  7729 
  7730   /* Set iterator position and end position.  */
  7731   memset (&it->current, 0, sizeof it->current);
  7732   it->current.overlay_string_index = -1;
  7733   it->current.dpvec_index = -1;
  7734   eassert (charpos >= 0);
  7735 
  7736   /* If STRING is specified, use its multibyteness, otherwise use the
  7737      setting of MULTIBYTE, if specified.  */
  7738   if (multibyte >= 0)
  7739     it->multibyte_p = multibyte > 0;
  7740 
  7741   /* Bidirectional reordering of strings is controlled by the default
  7742      value of bidi-display-reordering.  Don't try to reorder while
  7743      loading loadup.el, as the necessary character property tables are
  7744      not yet available.  */
  7745   it->bidi_p =
  7746     !redisplay__inhibit_bidi
  7747     && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
  7748 
  7749   if (s == NULL)
  7750     {
  7751       eassert (STRINGP (string));
  7752       it->string = string;
  7753       it->s = NULL;
  7754       it->end_charpos = it->string_nchars = SCHARS (string);
  7755       it->method = GET_FROM_STRING;
  7756       it->current.string_pos = string_pos (charpos, string);
  7757 
  7758       if (it->bidi_p)
  7759         {
  7760           it->bidi_it.string.lstring = string;
  7761           it->bidi_it.string.s = NULL;
  7762           it->bidi_it.string.schars = it->end_charpos;
  7763           it->bidi_it.string.bufpos = 0;
  7764           it->bidi_it.string.from_disp_str = false;
  7765           it->bidi_it.string.unibyte = !it->multibyte_p;
  7766           it->bidi_it.w = it->w;
  7767           bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
  7768                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  7769         }
  7770     }
  7771   else
  7772     {
  7773       it->s = (const unsigned char *) s;
  7774       it->string = Qnil;
  7775 
  7776       /* Note that we use IT->current.pos, not it->current.string_pos,
  7777          for displaying C strings.  */
  7778       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  7779       if (it->multibyte_p)
  7780         {
  7781           it->current.pos = c_string_pos (charpos, s, true);
  7782           it->end_charpos = it->string_nchars = number_of_chars (s, true);
  7783         }
  7784       else
  7785         {
  7786           IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
  7787           it->end_charpos = it->string_nchars = strlen (s);
  7788         }
  7789 
  7790       if (it->bidi_p)
  7791         {
  7792           it->bidi_it.string.lstring = Qnil;
  7793           it->bidi_it.string.s = (const unsigned char *) s;
  7794           it->bidi_it.string.schars = it->end_charpos;
  7795           it->bidi_it.string.bufpos = 0;
  7796           it->bidi_it.string.from_disp_str = false;
  7797           it->bidi_it.string.unibyte = !it->multibyte_p;
  7798           it->bidi_it.w = it->w;
  7799           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7800                         &it->bidi_it);
  7801         }
  7802       it->method = GET_FROM_C_STRING;
  7803     }
  7804 
  7805   /* PRECISION > 0 means don't return more than PRECISION characters
  7806      from the string.  */
  7807   if (precision > 0 && it->end_charpos - charpos > precision)
  7808     {
  7809       it->end_charpos = it->string_nchars = charpos + precision;
  7810       if (it->bidi_p)
  7811         it->bidi_it.string.schars = it->end_charpos;
  7812     }
  7813 
  7814   /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
  7815      characters have been returned.  FIELD_WIDTH == 0 means don't pad,
  7816      FIELD_WIDTH < 0 means infinite field width.  This is useful for
  7817      padding with `-' at the end of a mode line.  */
  7818   if (field_width < 0)
  7819     field_width = DISP_INFINITY;
  7820   /* Implementation note: We deliberately don't enlarge
  7821      it->bidi_it.string.schars here to fit it->end_charpos, because
  7822      the bidi iterator cannot produce characters out of thin air.  */
  7823   if (field_width > it->end_charpos - charpos)
  7824     it->end_charpos = charpos + field_width;
  7825 
  7826   /* Use the standard display table for displaying strings.  */
  7827   if (DISP_TABLE_P (Vstandard_display_table))
  7828     it->dp = XCHAR_TABLE (Vstandard_display_table);
  7829 
  7830   it->stop_charpos = charpos;
  7831   it->prev_stop = charpos;
  7832   it->base_level_stop = 0;
  7833   if (it->bidi_p)
  7834     {
  7835       it->bidi_it.first_elt = true;
  7836       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7837       it->bidi_it.disp_pos = -1;
  7838     }
  7839   if (s == NULL && it->multibyte_p)
  7840     {
  7841       ptrdiff_t endpos = SCHARS (it->string);
  7842       if (endpos > it->end_charpos)
  7843         endpos = it->end_charpos;
  7844       composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
  7845                                     it->string);
  7846     }
  7847   CHECK_IT (it);
  7848 }
  7849 
  7850 
  7851 
  7852 /***********************************************************************
  7853                               Iteration
  7854 ***********************************************************************/
  7855 
  7856 /* Map enum it_method value to corresponding next_element_from_* function.  */
  7857 
  7858 typedef bool (*next_element_function) (struct it *);
  7859 
  7860 static next_element_function const get_next_element[NUM_IT_METHODS] =
  7861 {
  7862   next_element_from_buffer,
  7863   next_element_from_display_vector,
  7864   next_element_from_string,
  7865   next_element_from_c_string,
  7866   next_element_from_image,
  7867   next_element_from_stretch,
  7868   next_element_from_xwidget,
  7869 };
  7870 
  7871 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
  7872 
  7873 
  7874 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
  7875    (possibly with the following characters).
  7876 
  7877   Note: we pass -1 as the "resolved bidi level" when the iterator
  7878   doesn't have the bidi_p flag set, because in that case we really
  7879   don't know what is the directionality of the text, so we leave it to
  7880   the shaping engine to figure that out.  */
  7881 
  7882 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS)                 \
  7883   ((IT)->cmp_it.id >= 0                                                 \
  7884    || ((IT)->cmp_it.stop_pos == (CHARPOS)                               \
  7885        && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS,       \
  7886                                  END_CHARPOS, (IT)->w,                  \
  7887                                  (IT)->bidi_p                           \
  7888                                  ? (IT)->bidi_it.resolved_level         \
  7889                                  : -1,                                  \
  7890                                  FACE_FROM_ID_OR_NULL ((IT)->f,         \
  7891                                                        (IT)->face_id),  \
  7892                                  (IT)->string)))
  7893 
  7894 
  7895 /* Lookup the char-table Vglyphless_char_display for character C (-1
  7896    if we want information for no-font case), and return the display
  7897    method symbol.  By side-effect, update it->what and
  7898    it->glyphless_method.  This function is called from
  7899    get_next_display_element for each character element, and from
  7900    gui_produce_glyphs when no suitable font was found.  */
  7901 
  7902 Lisp_Object
  7903 lookup_glyphless_char_display (int c, struct it *it)
  7904 {
  7905   Lisp_Object glyphless_method = Qnil;
  7906 
  7907   if (CHAR_TABLE_P (Vglyphless_char_display)
  7908       && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
  7909     {
  7910       if (c >= 0)
  7911         glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
  7912       else
  7913         glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
  7914 
  7915       if (CONSP (glyphless_method))
  7916         glyphless_method = FRAME_WINDOW_P (it->f)
  7917           ? XCAR (glyphless_method)
  7918           : XCDR (glyphless_method);
  7919     }
  7920 
  7921  retry:
  7922   if (NILP (glyphless_method))
  7923     {
  7924       if (c >= 0)
  7925         /* The default is to display the character by a proper font.  */
  7926         return Qnil;
  7927       /* The default for the no-font case is to display an empty box.  */
  7928       glyphless_method = Qempty_box;
  7929     }
  7930   if (EQ (glyphless_method, Qzero_width))
  7931     {
  7932       if (c >= 0)
  7933         return glyphless_method;
  7934       /* This method can't be used for the no-font case.  */
  7935       glyphless_method = Qempty_box;
  7936     }
  7937   if (EQ (glyphless_method, Qthin_space))
  7938     it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
  7939   else if (EQ (glyphless_method, Qempty_box))
  7940     it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
  7941   else if (EQ (glyphless_method, Qhex_code))
  7942     it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
  7943   else if (STRINGP (glyphless_method))
  7944     it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
  7945   else
  7946     {
  7947       /* Invalid value.  We use the default method.  */
  7948       glyphless_method = Qnil;
  7949       goto retry;
  7950     }
  7951   it->what = IT_GLYPHLESS;
  7952   return glyphless_method;
  7953 }
  7954 
  7955 /* Merge escape glyph face and cache the result.  */
  7956 
  7957 static struct frame *last_escape_glyph_frame = NULL;
  7958 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  7959 static int last_escape_glyph_merged_face_id = 0;
  7960 
  7961 static int
  7962 merge_escape_glyph_face (struct it *it)
  7963 {
  7964   int face_id;
  7965 
  7966   if (it->f == last_escape_glyph_frame
  7967       && it->face_id == last_escape_glyph_face_id)
  7968     face_id = last_escape_glyph_merged_face_id;
  7969   else
  7970     {
  7971       /* Merge the `escape-glyph' face into the current face.  */
  7972       face_id = merge_faces (it->w, Qescape_glyph, 0, it->face_id);
  7973       last_escape_glyph_frame = it->f;
  7974       last_escape_glyph_face_id = it->face_id;
  7975       last_escape_glyph_merged_face_id = face_id;
  7976     }
  7977   return face_id;
  7978 }
  7979 
  7980 /* Likewise for glyphless glyph face.  */
  7981 
  7982 static struct frame *last_glyphless_glyph_frame = NULL;
  7983 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  7984 static int last_glyphless_glyph_merged_face_id = 0;
  7985 
  7986 int
  7987 merge_glyphless_glyph_face (struct it *it)
  7988 {
  7989   int face_id;
  7990 
  7991   if (it->f == last_glyphless_glyph_frame
  7992       && it->face_id == last_glyphless_glyph_face_id)
  7993     face_id = last_glyphless_glyph_merged_face_id;
  7994   else
  7995     {
  7996       /* Merge the `glyphless-char' face into the current face.  */
  7997       face_id = merge_faces (it->w, Qglyphless_char, 0, it->face_id);
  7998       last_glyphless_glyph_frame = it->f;
  7999       last_glyphless_glyph_face_id = it->face_id;
  8000       last_glyphless_glyph_merged_face_id = face_id;
  8001     }
  8002   return face_id;
  8003 }
  8004 
  8005 /* Forget the `escape-glyph' and `glyphless-char' faces.  This should
  8006    be called before redisplaying windows, and when the frame's face
  8007    cache is freed.  */
  8008 void
  8009 forget_escape_and_glyphless_faces (void)
  8010 {
  8011   last_escape_glyph_frame = NULL;
  8012   last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  8013   last_glyphless_glyph_frame = NULL;
  8014   last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8015 }
  8016 
  8017 /* Load IT's display element fields with information about the next
  8018    display element from the current position of IT.  Value is false if
  8019    end of buffer (or C string) is reached.  */
  8020 
  8021 static bool
  8022 get_next_display_element (struct it *it)
  8023 {
  8024   /* True means that we found a display element.  False means that
  8025      we hit the end of what we iterate over.  Performance note: the
  8026      function pointer `method' used here turns out to be faster than
  8027      using a sequence of if-statements.  */
  8028   bool success_p;
  8029 
  8030  get_next:
  8031   success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  8032 
  8033   if (it->what == IT_CHARACTER)
  8034     {
  8035       /* UAX#9, L4: "A character is depicted by a mirrored glyph if
  8036          and only if (a) the resolved directionality of that character
  8037          is R..."  */
  8038       /* FIXME: Do we need an exception for characters from display
  8039          tables?  */
  8040       if (it->bidi_p && it->bidi_it.type == STRONG_R
  8041           && !inhibit_bidi_mirroring)
  8042         it->c = bidi_mirror_char (it->c);
  8043       /* Map via display table or translate control characters.
  8044          IT->c, IT->len etc. have been set to the next character by
  8045          the function call above.  If we have a display table, and it
  8046          contains an entry for IT->c, translate it.  Don't do this if
  8047          IT->c itself comes from a display table, otherwise we could
  8048          end up in an infinite recursion.  (An alternative could be to
  8049          count the recursion depth of this function and signal an
  8050          error when a certain maximum depth is reached.)  Is it worth
  8051          it?  */
  8052       if (success_p && it->dpvec == NULL)
  8053         {
  8054           Lisp_Object dv;
  8055           struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
  8056           bool nonascii_space_p = false;
  8057           bool nonascii_hyphen_p = false;
  8058           int c = it->c;        /* This is the character to display.  */
  8059 
  8060           if (! it->multibyte_p && ! ASCII_CHAR_P (c))
  8061             {
  8062               eassert (SINGLE_BYTE_CHAR_P (c));
  8063               if (unibyte_display_via_language_environment)
  8064                 {
  8065                   c = DECODE_CHAR (unibyte, c);
  8066                   if (c < 0)
  8067                     c = BYTE8_TO_CHAR (it->c);
  8068                 }
  8069               else
  8070                 c = BYTE8_TO_CHAR (it->c);
  8071             }
  8072 
  8073           if (it->dp
  8074               && (dv = DISP_CHAR_VECTOR (it->dp, c),
  8075                   VECTORP (dv)))
  8076             {
  8077               struct Lisp_Vector *v = XVECTOR (dv);
  8078 
  8079               /* Return the first character from the display table
  8080                  entry, if not empty.  If empty, don't display the
  8081                  current character.  */
  8082               if (v->header.size)
  8083                 {
  8084                   it->dpvec_char_len = it->len;
  8085                   it->dpvec = v->contents;
  8086                   it->dpend = v->contents + v->header.size;
  8087                   it->current.dpvec_index = 0;
  8088                   it->dpvec_face_id = -1;
  8089                   it->saved_face_id = it->face_id;
  8090                   it->method = GET_FROM_DISPLAY_VECTOR;
  8091                   it->ellipsis_p = false;
  8092                 }
  8093               else
  8094                 {
  8095                   set_iterator_to_next (it, false);
  8096                 }
  8097               goto get_next;
  8098             }
  8099 
  8100           if (! NILP (lookup_glyphless_char_display (c, it)))
  8101             {
  8102               if (it->what == IT_GLYPHLESS)
  8103                 goto done;
  8104               /* Don't display this character.  */
  8105               set_iterator_to_next (it, false);
  8106               goto get_next;
  8107             }
  8108 
  8109           /* If `nobreak-char-display' is non-nil, we display
  8110              non-ASCII spaces and hyphens specially.  */
  8111           if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
  8112             {
  8113               if (blankp (c))
  8114                 nonascii_space_p = true;
  8115               else if (c == SOFT_HYPHEN || c == HYPHEN
  8116                        || c == NON_BREAKING_HYPHEN)
  8117                 nonascii_hyphen_p = true;
  8118             }
  8119 
  8120           /* Translate control characters into `\003' or `^C' form.
  8121              Control characters coming from a display table entry are
  8122              currently not translated because we use IT->dpvec to hold
  8123              the translation.  This could easily be changed but I
  8124              don't believe that it is worth doing.
  8125 
  8126              The characters handled by `nobreak-char-display' must be
  8127              translated too.
  8128 
  8129              Non-printable characters and raw-byte characters are also
  8130              translated to octal or hexadecimal form.  */
  8131           if (((c < ' ' || c == 127) /* ASCII control chars.  */
  8132                ? (it->area != TEXT_AREA
  8133                   /* In mode line, treat \n, \t like other crl chars.  */
  8134                   || (c != '\t'
  8135                       && it->glyph_row
  8136                       && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
  8137                   || (c != '\n' && c != '\t'))
  8138                : (nonascii_space_p
  8139                   || nonascii_hyphen_p
  8140                   || CHAR_BYTE8_P (c)
  8141                   || ! CHAR_PRINTABLE_P (c))))
  8142             {
  8143               /* C is a control character, non-ASCII space/hyphen,
  8144                  raw-byte, or a non-printable character which must be
  8145                  displayed either as '\003' or as `^C' where the '\\'
  8146                  and '^' can be defined in the display table.  Fill
  8147                  IT->ctl_chars with glyphs for what we have to
  8148                  display.  Then, set IT->dpvec to these glyphs.  */
  8149               Lisp_Object gc;
  8150               int ctl_len;
  8151               int face_id;
  8152               int lface_id = 0;
  8153               int escape_glyph;
  8154 
  8155               /* Handle control characters with ^.  */
  8156 
  8157               if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
  8158                 {
  8159                   int g;
  8160 
  8161                   g = '^';           /* default glyph for Control */
  8162                   /* Set IT->ctl_chars[0] to the glyph for `^'.  */
  8163                   if (it->dp
  8164                       && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8165                     {
  8166                       g = GLYPH_CODE_CHAR (gc);
  8167                       lface_id = GLYPH_CODE_FACE (gc);
  8168                     }
  8169 
  8170                   face_id = (lface_id
  8171                              ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8172                              : merge_escape_glyph_face (it));
  8173 
  8174                   XSETINT (it->ctl_chars[0], g);
  8175                   XSETINT (it->ctl_chars[1], c ^ 0100);
  8176                   ctl_len = 2;
  8177                   goto display_control;
  8178                 }
  8179 
  8180               /* Handle non-ascii space in the mode where it only gets
  8181                  highlighting.  */
  8182 
  8183               if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
  8184                 {
  8185                   /* Merge `nobreak-space' into the current face.  */
  8186                   face_id = merge_faces (it->w, Qnobreak_space, 0,
  8187                                          it->face_id);
  8188                   XSETINT (it->ctl_chars[0],
  8189                            nobreak_char_ascii_display ? ' ' : it->c);
  8190                   ctl_len = 1;
  8191                   goto display_control;
  8192                 }
  8193 
  8194               /* Handle non-ascii hyphens in the mode where it only
  8195                  gets highlighting.  */
  8196 
  8197               if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
  8198                 {
  8199                   /* Merge `nobreak-space' into the current face.  */
  8200                   face_id = merge_faces (it->w, Qnobreak_hyphen, 0,
  8201                                          it->face_id);
  8202                   XSETINT (it->ctl_chars[0],
  8203                            nobreak_char_ascii_display ? '-' : it->c);
  8204                   ctl_len = 1;
  8205                   goto display_control;
  8206                 }
  8207 
  8208               /* Handle sequences that start with the "escape glyph".  */
  8209 
  8210               /* the default escape glyph is \.  */
  8211               escape_glyph = '\\';
  8212 
  8213               if (it->dp
  8214                   && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8215                 {
  8216                   escape_glyph = GLYPH_CODE_CHAR (gc);
  8217                   lface_id = GLYPH_CODE_FACE (gc);
  8218                 }
  8219 
  8220               face_id = (lface_id
  8221                          ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8222                          : merge_escape_glyph_face (it));
  8223 
  8224               /* Draw non-ASCII space/hyphen with escape glyph: */
  8225 
  8226               if (nonascii_space_p || nonascii_hyphen_p)
  8227                 {
  8228                   XSETINT (it->ctl_chars[0], escape_glyph);
  8229                   XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
  8230                   ctl_len = 2;
  8231                   goto display_control;
  8232                 }
  8233 
  8234               {
  8235                 char str[10];
  8236                 int len, i;
  8237 
  8238                 if (CHAR_BYTE8_P (c))
  8239                   /* Display \200 or \x80 instead of \17777600.  */
  8240                   c = CHAR_TO_BYTE8 (c);
  8241                 const char *format_string = display_raw_bytes_as_hex
  8242                                             ? "x%02x"
  8243                                             : "%03o";
  8244                 len = sprintf (str, format_string, c + 0u);
  8245 
  8246                 XSETINT (it->ctl_chars[0], escape_glyph);
  8247                 for (i = 0; i < len; i++)
  8248                   XSETINT (it->ctl_chars[i + 1], str[i]);
  8249                 ctl_len = len + 1;
  8250               }
  8251 
  8252             display_control:
  8253               /* Set up IT->dpvec and return first character from it.  */
  8254               it->dpvec_char_len = it->len;
  8255               it->dpvec = it->ctl_chars;
  8256               it->dpend = it->dpvec + ctl_len;
  8257               it->current.dpvec_index = 0;
  8258               it->dpvec_face_id = face_id;
  8259               it->saved_face_id = it->face_id;
  8260               it->method = GET_FROM_DISPLAY_VECTOR;
  8261               it->ellipsis_p = false;
  8262               goto get_next;
  8263             }
  8264           it->char_to_display = c;
  8265         }
  8266       else if (success_p)
  8267         {
  8268           it->char_to_display = it->c;
  8269         }
  8270     }
  8271 
  8272 #ifdef HAVE_WINDOW_SYSTEM
  8273   /* Adjust face id for a multibyte character.  There are no multibyte
  8274      character in unibyte text.  */
  8275   if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
  8276       && it->multibyte_p
  8277       && success_p
  8278       && FRAME_WINDOW_P (it->f))
  8279     {
  8280       struct face *face = FACE_FROM_ID (it->f, it->face_id);
  8281 
  8282       if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
  8283         {
  8284           /* Automatic composition with glyph-string.   */
  8285           Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
  8286 
  8287           it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
  8288         }
  8289       else
  8290         {
  8291           ptrdiff_t pos = (it->s ? -1
  8292                      : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
  8293                      : IT_CHARPOS (*it));
  8294           int c;
  8295 
  8296           if (it->what == IT_CHARACTER)
  8297             c = it->char_to_display;
  8298           else
  8299             {
  8300               struct composition *cmp = composition_table[it->cmp_it.id];
  8301               int i;
  8302 
  8303               c = ' ';
  8304               for (i = 0; i < cmp->glyph_len; i++)
  8305                 /* TAB in a composition means display glyphs with
  8306                    padding space on the left or right.  */
  8307                 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
  8308                   break;
  8309             }
  8310           it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
  8311         }
  8312     }
  8313 #endif  /* HAVE_WINDOW_SYSTEM */
  8314 
  8315  done:
  8316   /* Is this character the last one of a run of characters with
  8317      box?  If yes, set IT->end_of_box_run_p to true.  */
  8318   if (it->face_box_p
  8319       && it->s == NULL)
  8320     {
  8321       if (it->method == GET_FROM_STRING && it->sp)
  8322         {
  8323           int face_id = underlying_face_id (it);
  8324           struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
  8325 
  8326           if (face)
  8327             {
  8328               if (face->box == FACE_NO_BOX)
  8329                 {
  8330                   /* If the box comes from face properties in a
  8331                      display string, check faces in that string.  */
  8332                   int string_face_id = face_after_it_pos (it);
  8333                   if (FACE_FROM_ID (it->f, string_face_id)->box == FACE_NO_BOX)
  8334                     it->end_of_box_run_p = true;
  8335                 }
  8336               /* Otherwise, the box comes from the underlying face.
  8337                  If this is the last string character displayed, check
  8338                  the next buffer location.  */
  8339               else if (((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
  8340                         /* For a composition, see if the string ends
  8341                            at the last character included in the
  8342                            composition.  */
  8343                         || (it->what == IT_COMPOSITION
  8344                             && (IT_STRING_CHARPOS (*it) + it->cmp_it.nchars
  8345                                 >= SCHARS (it->string))))
  8346                        /* n_overlay_strings is unreliable unless
  8347                           overlay_string_index is non-negative.  */
  8348                        && ((it->current.overlay_string_index >= 0
  8349                             && (it->current.overlay_string_index
  8350                                 == it->n_overlay_strings - 1))
  8351                            /* A string from display property.  */
  8352                            || it->from_disp_prop_p))
  8353                 {
  8354                   ptrdiff_t ignore;
  8355                   int next_face_id;
  8356                   bool text_from_string = false;
  8357                   /* Normally, the next buffer location is stored in
  8358                      IT->current.pos...  */
  8359                   struct text_pos pos = it->current.pos;
  8360 
  8361                   /* ...but for a string from a display property, the
  8362                      next buffer position is stored in the 'position'
  8363                      member of the iteration stack slot below the
  8364                      current one, see handle_single_display_spec.  By
  8365                      contrast, it->current.pos was not yet updated to
  8366                      point to that buffer position; that will happen
  8367                      in pop_it, after we finish displaying the current
  8368                      string.  Note that we already checked above that
  8369                      it->sp is positive, so subtracting one from it is
  8370                      safe.  */
  8371                   if (it->from_disp_prop_p)
  8372                     {
  8373                       int stackp = it->sp - 1;
  8374 
  8375                       /* Find the stack level with data from buffer.  */
  8376                       while (stackp >= 0
  8377                              && STRINGP ((it->stack + stackp)->string))
  8378                         stackp--;
  8379                       if (stackp < 0)
  8380                         {
  8381                           /* If no stack slot was found for iterating
  8382                              a buffer, we are displaying text from a
  8383                              string, most probably the mode line or
  8384                              the header line, and that string has a
  8385                              display string on some of its
  8386                              characters.  */
  8387                           text_from_string = true;
  8388                           pos = it->stack[it->sp - 1].position;
  8389                         }
  8390                       else
  8391                         pos = (it->stack + stackp)->position;
  8392                     }
  8393                   else
  8394                     INC_TEXT_POS (pos, it->multibyte_p);
  8395 
  8396                   if (text_from_string)
  8397                     {
  8398                       Lisp_Object base_string = it->stack[it->sp - 1].string;
  8399 
  8400                       if (CHARPOS (pos) >= SCHARS (base_string) - 1)
  8401                         it->end_of_box_run_p = true;
  8402                       else
  8403                         {
  8404                           next_face_id
  8405                             = face_at_string_position (it->w, base_string,
  8406                                                        CHARPOS (pos), 0,
  8407                                                        &ignore, face_id,
  8408                                                        false, 0);
  8409                           if (FACE_FROM_ID (it->f, next_face_id)->box
  8410                               == FACE_NO_BOX)
  8411                             it->end_of_box_run_p = true;
  8412                         }
  8413                     }
  8414                   else if (CHARPOS (pos) >= ZV)
  8415                     it->end_of_box_run_p = true;
  8416                   else
  8417                     {
  8418                       next_face_id =
  8419                         face_at_buffer_position (it->w, CHARPOS (pos),
  8420                                                  &ignore,
  8421                                                  CHARPOS (pos)
  8422                                                  + TEXT_PROP_DISTANCE_LIMIT,
  8423                                                  false, -1, 0);
  8424                       if (FACE_FROM_ID (it->f, next_face_id)->box
  8425                           == FACE_NO_BOX)
  8426                         it->end_of_box_run_p = true;
  8427                     }
  8428                 }
  8429             }
  8430         }
  8431       /* next_element_from_display_vector sets this flag according to
  8432          faces of the display vector glyphs, see there.  */
  8433       else if (it->method != GET_FROM_DISPLAY_VECTOR)
  8434         {
  8435           int face_id = face_after_it_pos (it);
  8436           if (face_id != it->face_id
  8437               && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX)
  8438             it->end_of_box_run_p = true;
  8439         }
  8440     }
  8441   /* If we reached the end of the object we've been iterating (e.g., a
  8442      display string or an overlay string), and there's something on
  8443      IT->stack, proceed with what's on the stack.  It doesn't make
  8444      sense to return false if there's unprocessed stuff on the stack,
  8445      because otherwise that stuff will never be displayed.  */
  8446   if (!success_p && it->sp > 0)
  8447     {
  8448       set_iterator_to_next (it, false);
  8449       success_p = get_next_display_element (it);
  8450     }
  8451 
  8452   /* Value is false if end of buffer or string reached.  */
  8453   return success_p;
  8454 }
  8455 
  8456 
  8457 /* Move IT to the next display element.
  8458 
  8459    RESEAT_P means if called on a newline in buffer text,
  8460    skip to the next visible line start.
  8461 
  8462    Functions get_next_display_element and set_iterator_to_next are
  8463    separate because I find this arrangement easier to handle than a
  8464    get_next_display_element function that also increments IT's
  8465    position.  The way it is we can first look at an iterator's current
  8466    display element, decide whether it fits on a line, and if it does,
  8467    increment the iterator position.  The other way around we probably
  8468    would either need a flag indicating whether the iterator has to be
  8469    incremented the next time, or we would have to implement a
  8470    decrement position function which would not be easy to write.  */
  8471 
  8472 void
  8473 set_iterator_to_next (struct it *it, bool reseat_p)
  8474 {
  8475 
  8476   if (max_redisplay_ticks > 0)
  8477     update_redisplay_ticks (1, it->w);
  8478 
  8479   switch (it->method)
  8480     {
  8481     case GET_FROM_BUFFER:
  8482       /* The current display element of IT is a character from
  8483          current_buffer.  Advance in the buffer, and maybe skip over
  8484          invisible lines that are so because of selective display.  */
  8485       if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
  8486         reseat_at_next_visible_line_start (it, false);
  8487       else if (it->cmp_it.id >= 0)
  8488         {
  8489           /* We are currently getting glyphs from a composition.  */
  8490           if (! it->bidi_p)
  8491             {
  8492               IT_CHARPOS (*it) += it->cmp_it.nchars;
  8493               IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  8494             }
  8495           else
  8496             {
  8497               int i;
  8498 
  8499               /* Update IT's char/byte positions to point to the first
  8500                  character of the next grapheme cluster, or to the
  8501                  character visually after the current composition.  */
  8502               for (i = 0; i < it->cmp_it.nchars; i++)
  8503                 bidi_move_to_visually_next (&it->bidi_it);
  8504               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8505               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8506             }
  8507 
  8508           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8509               && it->cmp_it.to < it->cmp_it.nglyphs)
  8510             {
  8511               /* Composition created while scanning forward.  Proceed
  8512                  to the next grapheme cluster.  */
  8513               it->cmp_it.from = it->cmp_it.to;
  8514             }
  8515           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8516                    && it->cmp_it.from > 0)
  8517             {
  8518               /* Composition created while scanning backward.  Proceed
  8519                  to the previous grapheme cluster.  */
  8520               it->cmp_it.to = it->cmp_it.from;
  8521             }
  8522           else
  8523             {
  8524               /* No more grapheme clusters in this composition.
  8525                  Find the next stop position.  */
  8526               ptrdiff_t stop = it->end_charpos;
  8527 
  8528               if (it->bidi_it.scan_dir < 0)
  8529                 /* Now we are scanning backward and don't know
  8530                    where to stop.  */
  8531                 stop = -1;
  8532               composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8533                                             IT_BYTEPOS (*it), stop, Qnil);
  8534             }
  8535         }
  8536       else
  8537         {
  8538           eassert (it->len != 0);
  8539 
  8540           if (!it->bidi_p)
  8541             {
  8542               IT_BYTEPOS (*it) += it->len;
  8543               IT_CHARPOS (*it) += 1;
  8544             }
  8545           else
  8546             {
  8547               int prev_scan_dir = it->bidi_it.scan_dir;
  8548               /* If this is a new paragraph, determine its base
  8549                  direction (a.k.a. its base embedding level).  */
  8550               if (it->bidi_it.new_paragraph)
  8551                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  8552                                      false);
  8553               bidi_move_to_visually_next (&it->bidi_it);
  8554               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8555               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8556               if (prev_scan_dir != it->bidi_it.scan_dir)
  8557                 {
  8558                   /* As the scan direction was changed, we must
  8559                      re-compute the stop position for composition.  */
  8560                   ptrdiff_t stop = it->end_charpos;
  8561                   if (it->bidi_it.scan_dir < 0)
  8562                     stop = -1;
  8563                   composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8564                                                 IT_BYTEPOS (*it), stop, Qnil);
  8565                 }
  8566             }
  8567           eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
  8568         }
  8569       break;
  8570 
  8571     case GET_FROM_C_STRING:
  8572       /* Current display element of IT is from a C string.  */
  8573       if (!it->bidi_p
  8574           /* If the string position is beyond string's end, it means
  8575              next_element_from_c_string is padding the string with
  8576              blanks, in which case we bypass the bidi iterator,
  8577              because it cannot deal with such virtual characters.  */
  8578           || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
  8579         {
  8580           IT_BYTEPOS (*it) += it->len;
  8581           IT_CHARPOS (*it) += 1;
  8582         }
  8583       else
  8584         {
  8585           bidi_move_to_visually_next (&it->bidi_it);
  8586           IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8587           IT_CHARPOS (*it) = it->bidi_it.charpos;
  8588         }
  8589       break;
  8590 
  8591     case GET_FROM_DISPLAY_VECTOR:
  8592       /* Current display element of IT is from a display table entry.
  8593          Advance in the display table definition.  Reset it to null if
  8594          end reached, and continue with characters from buffers/
  8595          strings.  */
  8596       ++it->current.dpvec_index;
  8597 
  8598       /* Restore face of the iterator to what they were before the
  8599          display vector entry (these entries may contain faces).  */
  8600       it->face_id = it->saved_face_id;
  8601 
  8602       if (it->dpvec + it->current.dpvec_index >= it->dpend)
  8603         {
  8604           bool recheck_faces = it->ellipsis_p;
  8605 
  8606           if (it->s)
  8607             it->method = GET_FROM_C_STRING;
  8608           else if (STRINGP (it->string))
  8609             it->method = GET_FROM_STRING;
  8610           else
  8611             {
  8612               it->method = GET_FROM_BUFFER;
  8613               it->object = it->w->contents;
  8614             }
  8615 
  8616           it->dpvec = NULL;
  8617           it->current.dpvec_index = -1;
  8618 
  8619           /* Skip over characters which were displayed via IT->dpvec.  */
  8620           if (it->dpvec_char_len < 0)
  8621             reseat_at_next_visible_line_start (it, true);
  8622           else if (it->dpvec_char_len > 0)
  8623             {
  8624               it->len = it->dpvec_char_len;
  8625               set_iterator_to_next (it, reseat_p);
  8626             }
  8627 
  8628           /* Maybe recheck faces after display vector.  */
  8629           if (recheck_faces)
  8630             {
  8631               if (it->method == GET_FROM_STRING)
  8632                 it->stop_charpos = IT_STRING_CHARPOS (*it);
  8633               else
  8634                 it->stop_charpos = IT_CHARPOS (*it);
  8635             }
  8636         }
  8637       break;
  8638 
  8639     case GET_FROM_STRING:
  8640       /* Current display element is a character from a Lisp string.  */
  8641       eassert (it->s == NULL && STRINGP (it->string));
  8642       /* Don't advance past string end.  These conditions are true
  8643          when set_iterator_to_next is called at the end of
  8644          get_next_display_element, in which case the Lisp string is
  8645          already exhausted, and all we want is pop the iterator
  8646          stack.  */
  8647       if (it->current.overlay_string_index >= 0)
  8648         {
  8649           /* This is an overlay string, so there's no padding with
  8650              spaces, and the number of characters in the string is
  8651              where the string ends.  */
  8652           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8653             goto consider_string_end;
  8654         }
  8655       else
  8656         {
  8657           /* Not an overlay string.  There could be padding, so test
  8658              against it->end_charpos.  */
  8659           if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  8660             goto consider_string_end;
  8661         }
  8662       if (it->cmp_it.id >= 0)
  8663         {
  8664           /* We are delivering display elements from a composition.
  8665              Update the string position past the grapheme cluster
  8666              we've just processed.  */
  8667           if (! it->bidi_p)
  8668             {
  8669               IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  8670               IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  8671             }
  8672           else
  8673             {
  8674               int i;
  8675 
  8676               for (i = 0; i < it->cmp_it.nchars; i++)
  8677                 bidi_move_to_visually_next (&it->bidi_it);
  8678               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8679               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8680             }
  8681 
  8682           /* Did we exhaust all the grapheme clusters of this
  8683              composition?  */
  8684           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8685               && (it->cmp_it.to < it->cmp_it.nglyphs))
  8686             {
  8687               /* Not all the grapheme clusters were processed yet;
  8688                  advance to the next cluster.  */
  8689               it->cmp_it.from = it->cmp_it.to;
  8690             }
  8691           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8692                    && it->cmp_it.from > 0)
  8693             {
  8694               /* Likewise: advance to the next cluster, but going in
  8695                  the reverse direction.  */
  8696               it->cmp_it.to = it->cmp_it.from;
  8697             }
  8698           else
  8699             {
  8700               /* This composition was fully processed; find the next
  8701                  candidate place for checking for composed
  8702                  characters.  */
  8703               /* Always limit string searches to the string length;
  8704                  any padding spaces are not part of the string, and
  8705                  there cannot be any compositions in that padding.  */
  8706               ptrdiff_t stop = SCHARS (it->string);
  8707 
  8708               if (it->bidi_p && it->bidi_it.scan_dir < 0)
  8709                 stop = -1;
  8710               else if (it->end_charpos < stop)
  8711                 {
  8712                   /* Cf. PRECISION in reseat_to_string: we might be
  8713                      limited in how many of the string characters we
  8714                      need to deliver.  */
  8715                   stop = it->end_charpos;
  8716                 }
  8717               composition_compute_stop_pos (&it->cmp_it,
  8718                                             IT_STRING_CHARPOS (*it),
  8719                                             IT_STRING_BYTEPOS (*it), stop,
  8720                                             it->string);
  8721             }
  8722         }
  8723       else
  8724         {
  8725           if (!it->bidi_p
  8726               /* If the string position is beyond string's end, it
  8727                  means next_element_from_string is padding the string
  8728                  with blanks, in which case we bypass the bidi
  8729                  iterator, because it cannot deal with such virtual
  8730                  characters.  */
  8731               || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
  8732             {
  8733               IT_STRING_BYTEPOS (*it) += it->len;
  8734               IT_STRING_CHARPOS (*it) += 1;
  8735             }
  8736           else
  8737             {
  8738               int prev_scan_dir = it->bidi_it.scan_dir;
  8739 
  8740               bidi_move_to_visually_next (&it->bidi_it);
  8741               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8742               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8743               /* If the scan direction changes, we may need to update
  8744                  the place where to check for composed characters.  */
  8745               if (prev_scan_dir != it->bidi_it.scan_dir)
  8746                 {
  8747                   ptrdiff_t stop = SCHARS (it->string);
  8748 
  8749                   if (it->bidi_it.scan_dir < 0)
  8750                     stop = -1;
  8751                   else if (it->end_charpos < stop)
  8752                     stop = it->end_charpos;
  8753 
  8754                   composition_compute_stop_pos (&it->cmp_it,
  8755                                                 IT_STRING_CHARPOS (*it),
  8756                                                 IT_STRING_BYTEPOS (*it), stop,
  8757                                                 it->string);
  8758                 }
  8759             }
  8760         }
  8761 
  8762     consider_string_end:
  8763 
  8764       if (it->current.overlay_string_index >= 0)
  8765         {
  8766           /* IT->string is an overlay string.  Advance to the
  8767              next, if there is one.  */
  8768           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8769             {
  8770               it->ellipsis_p = false;
  8771               next_overlay_string (it);
  8772               if (it->ellipsis_p)
  8773                 setup_for_ellipsis (it, 0);
  8774             }
  8775         }
  8776       else
  8777         {
  8778           /* IT->string is not an overlay string.  If we reached
  8779              its end, and there is something on IT->stack, proceed
  8780              with what is on the stack.  This can be either another
  8781              string, this time an overlay string, or a buffer.  */
  8782           if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
  8783               && it->sp > 0)
  8784             {
  8785               pop_it (it);
  8786               if (it->method == GET_FROM_STRING)
  8787                 goto consider_string_end;
  8788             }
  8789         }
  8790       break;
  8791 
  8792     case GET_FROM_IMAGE:
  8793     case GET_FROM_STRETCH:
  8794     case GET_FROM_XWIDGET:
  8795 
  8796       /* The position etc with which we have to proceed are on
  8797          the stack.  The position may be at the end of a string,
  8798          if the `display' property takes up the whole string.  */
  8799       eassert (it->sp > 0);
  8800       pop_it (it);
  8801       if (it->method == GET_FROM_STRING)
  8802         goto consider_string_end;
  8803       break;
  8804 
  8805     default:
  8806       /* There are no other methods defined, so this should be a bug.  */
  8807       emacs_abort ();
  8808     }
  8809 
  8810   eassert (it->method != GET_FROM_STRING
  8811            || (STRINGP (it->string)
  8812                && IT_STRING_CHARPOS (*it) >= 0));
  8813 }
  8814 
  8815 /* Load IT's display element fields with information about the next
  8816    display element which comes from a display table entry or from the
  8817    result of translating a control character to one of the forms `^C'
  8818    or `\003'.
  8819 
  8820    IT->dpvec holds the glyphs to return as characters.
  8821    IT->saved_face_id holds the face id before the display vector--it
  8822    is restored into IT->face_id in set_iterator_to_next.  */
  8823 
  8824 static bool
  8825 next_element_from_display_vector (struct it *it)
  8826 {
  8827   Lisp_Object gc;
  8828   int prev_face_id = it->face_id;
  8829   int next_face_id;
  8830 
  8831   /* Precondition.  */
  8832   eassert (it->dpvec && it->current.dpvec_index >= 0);
  8833 
  8834   it->face_id = it->saved_face_id;
  8835 
  8836   /* KFS: This code used to check ip->dpvec[0] instead of the current element.
  8837      That seemed totally bogus - so I changed it...  */
  8838   if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
  8839       && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
  8840     {
  8841       struct face *this_face, *prev_face, *next_face;
  8842 
  8843       it->c = GLYPH_CODE_CHAR (gc);
  8844       it->len = CHAR_BYTES (it->c);
  8845 
  8846       /* The entry may contain a face id to use.  Such a face id is
  8847          the id of a Lisp face, not a realized face.  A face id of
  8848          zero means no face is specified.  */
  8849       if (it->dpvec_face_id >= 0)
  8850         it->face_id = it->dpvec_face_id;
  8851       else
  8852         {
  8853           int lface_id = GLYPH_CODE_FACE (gc);
  8854           if (lface_id > 0)
  8855             it->face_id = merge_faces (it->w, Qt, lface_id,
  8856                                        it->saved_face_id);
  8857         }
  8858 
  8859       /* Glyphs in the display vector could have the box face, so we
  8860          need to set the related flags in the iterator, as
  8861          appropriate.  */
  8862       this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8863       prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  8864 
  8865       /* Is this character the first character of a box-face run?  */
  8866       it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
  8867                                 && (!prev_face
  8868                                     || prev_face->box == FACE_NO_BOX));
  8869 
  8870       /* For the last character of the box-face run, we need to look
  8871          either at the next glyph from the display vector, or at the
  8872          face we saw before the display vector.  */
  8873       next_face_id = it->saved_face_id;
  8874       if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
  8875         {
  8876           if (it->dpvec_face_id >= 0)
  8877             next_face_id = it->dpvec_face_id;
  8878           else
  8879             {
  8880               Lisp_Object gc = it->dpvec[it->current.dpvec_index + 1];
  8881               int lface_id = GLYPH_CODE_P (gc) ? GLYPH_CODE_FACE (gc) : 0;
  8882 
  8883               if (lface_id > 0)
  8884                 next_face_id = merge_faces (it->w, Qt, lface_id,
  8885                                             it->saved_face_id);
  8886             }
  8887         }
  8888       next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
  8889       if (this_face && this_face->box != FACE_NO_BOX
  8890           && (!next_face || next_face->box == FACE_NO_BOX))
  8891         it->end_of_box_run_p = true;
  8892       it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
  8893     }
  8894   else
  8895     /* Display table entry is invalid.  Return a space.  */
  8896     it->c = ' ', it->len = 1;
  8897 
  8898   /* Don't change position and object of the iterator here.  They are
  8899      still the values of the character that had this display table
  8900      entry or was translated, and that's what we want.  */
  8901   it->what = IT_CHARACTER;
  8902   return true;
  8903 }
  8904 
  8905 /* Get the first element of string/buffer in the visual order, after
  8906    being reseated to a new position in a string or a buffer.  */
  8907 static void
  8908 get_visually_first_element (struct it *it)
  8909 {
  8910   bool string_p = STRINGP (it->string) || it->s;
  8911   ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
  8912   ptrdiff_t bob;
  8913   ptrdiff_t obegv = BEGV;
  8914 
  8915   SET_WITH_NARROWED_BEGV (it, bob,
  8916                           string_p ? 0 :
  8917                           IT_CHARPOS (*it) < BEGV ? obegv : BEGV,
  8918                           it->medium_narrowing_begv);
  8919 
  8920   if (STRINGP (it->string))
  8921     {
  8922       it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
  8923       it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
  8924     }
  8925   else
  8926     {
  8927       it->bidi_it.charpos = IT_CHARPOS (*it);
  8928       it->bidi_it.bytepos = IT_BYTEPOS (*it);
  8929     }
  8930 
  8931   if (it->bidi_it.charpos == eob)
  8932     {
  8933       /* Nothing to do, but reset the FIRST_ELT flag, like
  8934          bidi_paragraph_init does, because we are not going to
  8935          call it.  */
  8936       it->bidi_it.first_elt = false;
  8937     }
  8938   else if (it->bidi_it.charpos == bob
  8939            || (!string_p
  8940                && (FETCH_BYTE (it->bidi_it.bytepos - 1) == '\n'
  8941                    || FETCH_BYTE (it->bidi_it.bytepos) == '\n')))
  8942     {
  8943       /* If we are at the beginning of a line/string, we can produce
  8944          the next element right away.  */
  8945       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  8946       bidi_move_to_visually_next (&it->bidi_it);
  8947     }
  8948   else
  8949     {
  8950       ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
  8951 
  8952       /* We need to prime the bidi iterator starting at the line's or
  8953          string's beginning, before we will be able to produce the
  8954          next element.  */
  8955       if (string_p)
  8956         it->bidi_it.charpos = it->bidi_it.bytepos = 0;
  8957       else
  8958         SET_WITH_NARROWED_BEGV (it, it->bidi_it.charpos,
  8959                                 find_newline_no_quit (IT_CHARPOS (*it),
  8960                                                       IT_BYTEPOS (*it), -1,
  8961                                                       &it->bidi_it.bytepos),
  8962                                 it->medium_narrowing_begv);
  8963       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  8964       do
  8965         {
  8966           /* Now return to buffer/string position where we were asked
  8967              to get the next display element, and produce that.  */
  8968           bidi_move_to_visually_next (&it->bidi_it);
  8969         }
  8970       while (it->bidi_it.bytepos != orig_bytepos
  8971              && it->bidi_it.charpos < eob);
  8972     }
  8973 
  8974   /*  Adjust IT's position information to where we ended up.  */
  8975   if (STRINGP (it->string))
  8976     {
  8977       IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8978       IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8979     }
  8980   else
  8981     {
  8982       IT_CHARPOS (*it) = it->bidi_it.charpos;
  8983       IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8984     }
  8985 
  8986   if (STRINGP (it->string) || !it->s)
  8987     {
  8988       ptrdiff_t stop, charpos, bytepos;
  8989 
  8990       if (STRINGP (it->string))
  8991         {
  8992           eassert (!it->s);
  8993           stop = SCHARS (it->string);
  8994           if (stop > it->end_charpos)
  8995             stop = it->end_charpos;
  8996           charpos = IT_STRING_CHARPOS (*it);
  8997           bytepos = IT_STRING_BYTEPOS (*it);
  8998         }
  8999       else
  9000         {
  9001           stop = it->end_charpos;
  9002           charpos = IT_CHARPOS (*it);
  9003           bytepos = IT_BYTEPOS (*it);
  9004         }
  9005       if (it->bidi_it.scan_dir < 0)
  9006         stop = -1;
  9007       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
  9008                                     it->string);
  9009     }
  9010 }
  9011 
  9012 /* Load IT with the next display element from Lisp string IT->string.
  9013    IT->current.string_pos is the current position within the string.
  9014    If IT->current.overlay_string_index >= 0, the Lisp string is an
  9015    overlay string.  */
  9016 
  9017 static bool
  9018 next_element_from_string (struct it *it)
  9019 {
  9020   struct text_pos position;
  9021 
  9022   eassert (STRINGP (it->string));
  9023   eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
  9024   eassert (IT_STRING_CHARPOS (*it) >= 0);
  9025   position = it->current.string_pos;
  9026 
  9027   /* With bidi reordering, the character to display might not be the
  9028      character at IT_STRING_CHARPOS.  BIDI_IT.FIRST_ELT means
  9029      that we were reseat()ed to a new string, whose paragraph
  9030      direction is not known.  */
  9031   if (it->bidi_p && it->bidi_it.first_elt)
  9032     {
  9033       get_visually_first_element (it);
  9034       SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
  9035     }
  9036 
  9037   /* Time to check for invisible text?  */
  9038   if (IT_STRING_CHARPOS (*it) < it->end_charpos)
  9039     {
  9040       if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
  9041         {
  9042           if (!(!it->bidi_p
  9043                 || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9044                 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
  9045             {
  9046               /* With bidi non-linear iteration, we could find
  9047                  ourselves far beyond the last computed stop_charpos,
  9048                  with several other stop positions in between that we
  9049                  missed.  Scan them all now, in buffer's logical
  9050                  order, until we find and handle the last stop_charpos
  9051                  that precedes our current position.  */
  9052               handle_stop_backwards (it, it->stop_charpos);
  9053               return GET_NEXT_DISPLAY_ELEMENT (it);
  9054             }
  9055           else
  9056             {
  9057               if (it->bidi_p)
  9058                 {
  9059                   /* Take note of the stop position we just moved
  9060                      across, for when we will move back across it.  */
  9061                   it->prev_stop = it->stop_charpos;
  9062                   /* If we are at base paragraph embedding level, take
  9063                      note of the last stop position seen at this
  9064                      level.  */
  9065                   if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9066                     it->base_level_stop = it->stop_charpos;
  9067                 }
  9068               handle_stop (it);
  9069 
  9070               /* Since a handler may have changed IT->method, we must
  9071                  recurse here.  */
  9072               return GET_NEXT_DISPLAY_ELEMENT (it);
  9073             }
  9074         }
  9075       else if (it->bidi_p
  9076                /* If we are before prev_stop, we may have overstepped
  9077                   on our way backwards a stop_pos, and if so, we need
  9078                   to handle that stop_pos.  */
  9079                && IT_STRING_CHARPOS (*it) < it->prev_stop
  9080                /* We can sometimes back up for reasons that have nothing
  9081                   to do with bidi reordering.  E.g., compositions.  The
  9082                   code below is only needed when we are above the base
  9083                   embedding level, so test for that explicitly.  */
  9084                && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9085         {
  9086           /* If we lost track of base_level_stop, we have no better
  9087              place for handle_stop_backwards to start from than string
  9088              beginning.  This happens, e.g., when we were reseated to
  9089              the previous screenful of text by vertical-motion.  */
  9090           if (it->base_level_stop <= 0
  9091               || IT_STRING_CHARPOS (*it) < it->base_level_stop)
  9092             it->base_level_stop = 0;
  9093           handle_stop_backwards (it, it->base_level_stop);
  9094           return GET_NEXT_DISPLAY_ELEMENT (it);
  9095         }
  9096     }
  9097 
  9098   if (it->current.overlay_string_index >= 0)
  9099     {
  9100       /* Get the next character from an overlay string.  In overlay
  9101          strings, there is no field width or padding with spaces to
  9102          do.  */
  9103       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  9104         {
  9105           it->what = IT_EOB;
  9106           return false;
  9107         }
  9108       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9109                                 IT_STRING_BYTEPOS (*it),
  9110                                 it->bidi_it.scan_dir < 0
  9111                                 ? -1
  9112                                 : SCHARS (it->string))
  9113                && next_element_from_composition (it))
  9114         {
  9115           return true;
  9116         }
  9117       else if (STRING_MULTIBYTE (it->string))
  9118         {
  9119           const unsigned char *s = (SDATA (it->string)
  9120                                     + IT_STRING_BYTEPOS (*it));
  9121           it->c = check_char_and_length (s, &it->len);
  9122         }
  9123       else
  9124         {
  9125           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9126           it->len = 1;
  9127         }
  9128     }
  9129   else
  9130     {
  9131       /* Get the next character from a Lisp string that is not an
  9132          overlay string.  Such strings come from the mode line, for
  9133          example.  We may have to pad with spaces, or truncate the
  9134          string.  See also next_element_from_c_string.  */
  9135       if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  9136         {
  9137           it->what = IT_EOB;
  9138           return false;
  9139         }
  9140       else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
  9141         {
  9142           /* Pad with spaces.  */
  9143           it->c = ' ', it->len = 1;
  9144           CHARPOS (position) = BYTEPOS (position) = -1;
  9145         }
  9146       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9147                                 IT_STRING_BYTEPOS (*it),
  9148                                 it->bidi_it.scan_dir < 0
  9149                                 ? -1
  9150                                 : it->string_nchars)
  9151                && next_element_from_composition (it))
  9152         {
  9153           return true;
  9154         }
  9155       else if (STRING_MULTIBYTE (it->string))
  9156         {
  9157           const unsigned char *s = (SDATA (it->string)
  9158                                     + IT_STRING_BYTEPOS (*it));
  9159           it->c = check_char_and_length (s, &it->len);
  9160         }
  9161       else
  9162         {
  9163           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9164           it->len = 1;
  9165         }
  9166     }
  9167 
  9168   /* Record what we have and where it came from.  */
  9169   it->what = IT_CHARACTER;
  9170   it->object = it->string;
  9171   it->position = position;
  9172   return true;
  9173 }
  9174 
  9175 
  9176 /* Load IT with next display element from C string IT->s.
  9177    IT->string_nchars is the maximum number of characters to return
  9178    from the string.  IT->end_charpos may be greater than
  9179    IT->string_nchars when this function is called, in which case we
  9180    may have to return padding spaces.  Value is false if end of string
  9181    reached, including padding spaces.  */
  9182 
  9183 static bool
  9184 next_element_from_c_string (struct it *it)
  9185 {
  9186   bool success_p = true;
  9187 
  9188   eassert (it->s);
  9189   eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
  9190   it->what = IT_CHARACTER;
  9191   BYTEPOS (it->position) = CHARPOS (it->position) = 0;
  9192   it->object = make_fixnum (0);
  9193 
  9194   /* With bidi reordering, the character to display might not be the
  9195      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9196      we were reseated to a new string, whose paragraph direction is
  9197      not known.  */
  9198   if (it->bidi_p && it->bidi_it.first_elt)
  9199     get_visually_first_element (it);
  9200 
  9201   /* IT's position can be greater than IT->string_nchars in case a
  9202      field width or precision has been specified when the iterator was
  9203      initialized.  */
  9204   if (IT_CHARPOS (*it) >= it->end_charpos)
  9205     {
  9206       /* End of the game.  */
  9207       it->what = IT_EOB;
  9208       success_p = false;
  9209     }
  9210   else if (IT_CHARPOS (*it) >= it->string_nchars)
  9211     {
  9212       /* Pad with spaces.  */
  9213       it->c = ' ', it->len = 1;
  9214       BYTEPOS (it->position) = CHARPOS (it->position) = -1;
  9215     }
  9216   else if (it->multibyte_p)
  9217     it->c = check_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
  9218   else
  9219     it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
  9220 
  9221   return success_p;
  9222 }
  9223 
  9224 
  9225 /* Set up IT to return characters from an ellipsis, if appropriate.
  9226    The definition of the ellipsis glyphs may come from a display table
  9227    entry.  This function fills IT with the first glyph from the
  9228    ellipsis if an ellipsis is to be displayed.  */
  9229 
  9230 static bool
  9231 next_element_from_ellipsis (struct it *it)
  9232 {
  9233   if (it->selective_display_ellipsis_p)
  9234     setup_for_ellipsis (it, it->len);
  9235   else
  9236     {
  9237       /* The face at the current position may be different from the
  9238          face we find after the invisible text.  Remember what it
  9239          was in IT->saved_face_id, and signal that it's there by
  9240          setting face_before_selective_p.  */
  9241       it->saved_face_id = it->face_id;
  9242       it->method = GET_FROM_BUFFER;
  9243       it->object = it->w->contents;
  9244       reseat_at_next_visible_line_start (it, true);
  9245       it->face_before_selective_p = true;
  9246     }
  9247 
  9248   return GET_NEXT_DISPLAY_ELEMENT (it);
  9249 }
  9250 
  9251 
  9252 /* Deliver an image display element.  The iterator IT is already
  9253    filled with image information (done in handle_display_prop).  Value
  9254    is always true.  */
  9255 
  9256 
  9257 static bool
  9258 next_element_from_image (struct it *it)
  9259 {
  9260   it->what = IT_IMAGE;
  9261   return true;
  9262 }
  9263 
  9264 static bool
  9265 next_element_from_xwidget (struct it *it)
  9266 {
  9267   it->what = IT_XWIDGET;
  9268   return true;
  9269 }
  9270 
  9271 
  9272 /* Fill iterator IT with next display element from a stretch glyph
  9273    property.  IT->object is the value of the text property.  Value is
  9274    always true.  */
  9275 
  9276 static bool
  9277 next_element_from_stretch (struct it *it)
  9278 {
  9279   it->what = IT_STRETCH;
  9280   return true;
  9281 }
  9282 
  9283 /* Scan backwards from IT's current position until we find a stop
  9284    position, or until BEGV.  This is called when we find ourself
  9285    before both the last known prev_stop and base_level_stop while
  9286    reordering bidirectional text.  */
  9287 
  9288 static void
  9289 compute_stop_pos_backwards (struct it *it)
  9290 {
  9291   const int SCAN_BACK_LIMIT = 1000;
  9292   struct text_pos pos;
  9293   struct display_pos save_current = it->current;
  9294   struct text_pos save_position = it->position;
  9295   ptrdiff_t charpos = IT_CHARPOS (*it);
  9296   ptrdiff_t where_we_are = charpos;
  9297   ptrdiff_t save_stop_pos = it->stop_charpos;
  9298   ptrdiff_t save_end_pos = it->end_charpos;
  9299 
  9300   eassert (NILP (it->string) && !it->s);
  9301   eassert (it->bidi_p);
  9302   it->bidi_p = false;
  9303   do
  9304     {
  9305       it->end_charpos = min (charpos + 1, ZV);
  9306       charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
  9307       SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
  9308       reseat_1 (it, pos, false);
  9309       compute_stop_pos (it);
  9310       /* We must advance forward, right?  */
  9311       if (it->stop_charpos <= charpos)
  9312         emacs_abort ();
  9313     }
  9314   while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
  9315 
  9316   if (it->stop_charpos <= where_we_are)
  9317     it->prev_stop = it->stop_charpos;
  9318   else
  9319     it->prev_stop = BEGV;
  9320   it->bidi_p = true;
  9321   it->current = save_current;
  9322   it->position = save_position;
  9323   it->stop_charpos = save_stop_pos;
  9324   it->end_charpos = save_end_pos;
  9325 }
  9326 
  9327 /* Scan forward from CHARPOS in the current buffer/string, until we
  9328    find a stop position > current IT's position.  Then handle the stop
  9329    position before that.  This is called when we bump into a stop
  9330    position while reordering bidirectional text.  CHARPOS should be
  9331    the last previously processed stop_pos (or BEGV/0, if none were
  9332    processed yet) whose position is less than IT's current
  9333    position.  */
  9334 
  9335 static void
  9336 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
  9337 {
  9338   bool bufp = !STRINGP (it->string);
  9339   ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
  9340   struct display_pos save_current = it->current;
  9341   struct text_pos save_position = it->position;
  9342   struct composition_it save_cmp_it = it->cmp_it;
  9343   struct text_pos pos1;
  9344   ptrdiff_t next_stop;
  9345 
  9346   /* Scan in strict logical order.  */
  9347   eassert (it->bidi_p);
  9348   it->bidi_p = false;
  9349   do
  9350     {
  9351       it->prev_stop = charpos;
  9352       if (bufp)
  9353         {
  9354           SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
  9355           reseat_1 (it, pos1, false);
  9356         }
  9357       else
  9358         it->current.string_pos = string_pos (charpos, it->string);
  9359       compute_stop_pos (it);
  9360       /* We must advance forward, right?  */
  9361       if (it->stop_charpos <= it->prev_stop)
  9362         emacs_abort ();
  9363       charpos = it->stop_charpos;
  9364     }
  9365   while (charpos <= where_we_are);
  9366 
  9367   it->bidi_p = true;
  9368   it->current = save_current;
  9369   it->position = save_position;
  9370   it->cmp_it = save_cmp_it;
  9371   next_stop = it->stop_charpos;
  9372   it->stop_charpos = it->prev_stop;
  9373   handle_stop (it);
  9374   it->stop_charpos = next_stop;
  9375 }
  9376 
  9377 /* Load IT with the next display element from current_buffer.  Value
  9378    is false if end of buffer reached.  IT->stop_charpos is the next
  9379    position at which to stop and check for text properties or buffer
  9380    end.  */
  9381 
  9382 static bool
  9383 next_element_from_buffer (struct it *it)
  9384 {
  9385   bool success_p = true;
  9386 
  9387   eassert (IT_CHARPOS (*it) >= BEGV);
  9388   eassert (NILP (it->string) && !it->s);
  9389   eassert (!it->bidi_p
  9390            || (NILP (it->bidi_it.string.lstring)
  9391                && it->bidi_it.string.s == NULL));
  9392 
  9393   /* With bidi reordering, the character to display might not be the
  9394      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9395      we were reseat()ed to a new buffer position, which is potentially
  9396      a different paragraph.  */
  9397   if (it->bidi_p && it->bidi_it.first_elt)
  9398     {
  9399       get_visually_first_element (it);
  9400       SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9401     }
  9402 
  9403   if (IT_CHARPOS (*it) >= it->stop_charpos)
  9404     {
  9405       if (IT_CHARPOS (*it) >= it->end_charpos)
  9406         {
  9407           bool overlay_strings_follow_p;
  9408 
  9409           /* End of the game, except when overlay strings follow that
  9410              haven't been returned yet.  */
  9411           if (it->overlay_strings_at_end_processed_p)
  9412             overlay_strings_follow_p = false;
  9413           else
  9414             {
  9415               it->overlay_strings_at_end_processed_p = true;
  9416               overlay_strings_follow_p = get_overlay_strings (it, 0);
  9417             }
  9418 
  9419           if (overlay_strings_follow_p)
  9420             success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  9421           else
  9422             {
  9423               it->what = IT_EOB;
  9424               it->position = it->current.pos;
  9425               success_p = false;
  9426             }
  9427         }
  9428       else if (!(!it->bidi_p
  9429                  || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9430                  || IT_CHARPOS (*it) == it->stop_charpos))
  9431         {
  9432           /* With bidi non-linear iteration, we could find ourselves
  9433              far beyond the last computed stop_charpos, with several
  9434              other stop positions in between that we missed.  Scan
  9435              them all now, in buffer's logical order, until we find
  9436              and handle the last stop_charpos that precedes our
  9437              current position.  */
  9438           handle_stop_backwards (it, it->stop_charpos);
  9439           it->ignore_overlay_strings_at_pos_p = false;
  9440           return GET_NEXT_DISPLAY_ELEMENT (it);
  9441         }
  9442       else
  9443         {
  9444           if (it->bidi_p)
  9445             {
  9446               /* Take note of the stop position we just moved across,
  9447                  for when we will move back across it.  */
  9448               it->prev_stop = it->stop_charpos;
  9449               /* If we are at base paragraph embedding level, take
  9450                  note of the last stop position seen at this
  9451                  level.  */
  9452               if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9453                 it->base_level_stop = it->stop_charpos;
  9454             }
  9455           handle_stop (it);
  9456           it->ignore_overlay_strings_at_pos_p = false;
  9457           return GET_NEXT_DISPLAY_ELEMENT (it);
  9458         }
  9459     }
  9460   else if (it->bidi_p
  9461            /* If we are before prev_stop, we may have overstepped on
  9462               our way backwards a stop_pos, and if so, we need to
  9463               handle that stop_pos.  */
  9464            && IT_CHARPOS (*it) < it->prev_stop
  9465            /* We can sometimes back up for reasons that have nothing
  9466               to do with bidi reordering.  E.g., compositions.  The
  9467               code below is only needed when we are above the base
  9468               embedding level, so test for that explicitly.  */
  9469            && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9470     {
  9471       if (it->base_level_stop <= 0
  9472           || IT_CHARPOS (*it) < it->base_level_stop)
  9473         {
  9474           /* If we lost track of base_level_stop, we need to find
  9475              prev_stop by looking backwards.  This happens, e.g., when
  9476              we were reseated to the previous screenful of text by
  9477              vertical-motion.  */
  9478           it->base_level_stop = BEGV;
  9479           compute_stop_pos_backwards (it);
  9480           handle_stop_backwards (it, it->prev_stop);
  9481         }
  9482       else
  9483         handle_stop_backwards (it, it->base_level_stop);
  9484       it->ignore_overlay_strings_at_pos_p = false;
  9485       return GET_NEXT_DISPLAY_ELEMENT (it);
  9486     }
  9487   else
  9488     {
  9489       /* No face changes, overlays etc. in sight, so just return a
  9490          character from current_buffer.  */
  9491       unsigned char *p;
  9492       ptrdiff_t stop;
  9493 
  9494       /* We moved to the next buffer position, so any info about
  9495          previously seen overlays is no longer valid.  */
  9496       it->ignore_overlay_strings_at_pos_p = false;
  9497 
  9498       if (composition_break_at_point
  9499           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  9500           && !NILP (Vauto_composition_mode))
  9501         {
  9502           /* Limit search for composable characters to point's position.  */
  9503           if (it->bidi_it.scan_dir < 0)
  9504             stop = (PT <= IT_CHARPOS (*it)) ? PT : -1;
  9505           else
  9506             stop = (IT_CHARPOS (*it) < PT
  9507                     && PT < it->end_charpos) ? PT : it->end_charpos;
  9508         }
  9509       else
  9510         stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
  9511       if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
  9512                            stop)
  9513           && next_element_from_composition (it))
  9514         {
  9515           return true;
  9516         }
  9517 
  9518       /* Get the next character, maybe multibyte.  */
  9519       p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
  9520       if (it->multibyte_p && !ASCII_CHAR_P (*p))
  9521         it->c = string_char_and_length (p, &it->len);
  9522       else
  9523         it->c = *p, it->len = 1;
  9524 
  9525       /* Record what we have and where it came from.  */
  9526       it->what = IT_CHARACTER;
  9527       it->object = it->w->contents;
  9528       it->position = it->current.pos;
  9529 
  9530       /* Normally we return the character found above, except when we
  9531          really want to return an ellipsis for selective display.  */
  9532       if (it->selective)
  9533         {
  9534           if (it->c == '\n')
  9535             {
  9536               /* A value of selective > 0 means hide lines indented more
  9537                  than that number of columns.  */
  9538               if (it->selective > 0
  9539                   && IT_CHARPOS (*it) + 1 < ZV
  9540                   && indented_beyond_p (IT_CHARPOS (*it) + 1,
  9541                                         IT_BYTEPOS (*it) + 1,
  9542                                         it->selective))
  9543                 {
  9544                   success_p = next_element_from_ellipsis (it);
  9545                   it->dpvec_char_len = -1;
  9546                 }
  9547             }
  9548           else if (it->c == '\r' && it->selective == -1)
  9549             {
  9550               /* A value of selective == -1 means that everything from the
  9551                  CR to the end of the line is invisible, with maybe an
  9552                  ellipsis displayed for it.  */
  9553               success_p = next_element_from_ellipsis (it);
  9554               it->dpvec_char_len = -1;
  9555             }
  9556         }
  9557     }
  9558 
  9559   /* Value is false if end of buffer reached.  */
  9560   eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
  9561   return success_p;
  9562 }
  9563 
  9564 
  9565 /* Deliver a composition display element.  Unlike the other
  9566    next_element_from_XXX, this function is not registered in the array
  9567    get_next_element[].  It is called from next_element_from_buffer and
  9568    next_element_from_string when necessary.  */
  9569 
  9570 static bool
  9571 next_element_from_composition (struct it *it)
  9572 {
  9573   it->what = IT_COMPOSITION;
  9574   it->len = it->cmp_it.nbytes;
  9575   if (STRINGP (it->string))
  9576     {
  9577       if (it->c < 0)
  9578         {
  9579           IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  9580           IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  9581           return false;
  9582         }
  9583       it->position = it->current.string_pos;
  9584       it->object = it->string;
  9585       it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
  9586                                      IT_STRING_BYTEPOS (*it), it->string);
  9587     }
  9588   else
  9589     {
  9590       if (it->c < 0)
  9591         {
  9592           IT_CHARPOS (*it) += it->cmp_it.nchars;
  9593           IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  9594           if (it->bidi_p)
  9595             {
  9596               if (it->bidi_it.new_paragraph)
  9597                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  9598                                      false);
  9599               /* Resync the bidi iterator with IT's new position.
  9600                  FIXME: this doesn't support bidirectional text.  */
  9601               while (it->bidi_it.charpos < IT_CHARPOS (*it))
  9602                 bidi_move_to_visually_next (&it->bidi_it);
  9603             }
  9604           return false;
  9605         }
  9606       it->position = it->current.pos;
  9607       it->object = it->w->contents;
  9608       it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
  9609                                      IT_BYTEPOS (*it), Qnil);
  9610     }
  9611   return true;
  9612 }
  9613 
  9614 
  9615 
  9616 /***********************************************************************
  9617              Moving an iterator without producing glyphs
  9618  ***********************************************************************/
  9619 
  9620 /* Check if iterator is at a position corresponding to a valid buffer
  9621    position after some move_it_ call.  */
  9622 
  9623 #define IT_POS_VALID_AFTER_MOVE_P(it)                   \
  9624   ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
  9625 
  9626 
  9627 /* Move iterator IT to a specified buffer or X position within one
  9628    line on the display without producing glyphs.
  9629 
  9630    OP should be a bit mask including some or all of these bits:
  9631     MOVE_TO_X: Stop upon reaching x-position TO_X.
  9632     MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
  9633    Regardless of OP's value, stop upon reaching the end of the display line.
  9634 
  9635    TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
  9636    This means, in particular, that TO_X includes window's horizontal
  9637    scroll amount.
  9638 
  9639    The return value has several possible values that
  9640    say what condition caused the scan to stop:
  9641 
  9642    MOVE_POS_MATCH_OR_ZV
  9643      - when TO_POS or ZV was reached.
  9644 
  9645    MOVE_X_REACHED
  9646      -when TO_X was reached before TO_POS or ZV were reached.
  9647 
  9648    MOVE_LINE_CONTINUED
  9649      - when we reached the end of the display area and the line must
  9650      be continued.
  9651 
  9652    MOVE_LINE_TRUNCATED
  9653      - when we reached the end of the display area and the line is
  9654      truncated.
  9655 
  9656    MOVE_NEWLINE_OR_CR
  9657      - when we stopped at a line end, i.e. a newline or a CR and selective
  9658      display is on.  */
  9659 
  9660 static enum move_it_result
  9661 move_it_in_display_line_to (struct it *it,
  9662                             ptrdiff_t to_charpos, int to_x,
  9663                             enum move_operation_enum op)
  9664 {
  9665   enum move_it_result result = MOVE_UNDEFINED;
  9666   struct glyph_row *saved_glyph_row;
  9667   struct it wrap_it, atpos_it, atx_it, ppos_it;
  9668   void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
  9669   void *ppos_data = NULL;
  9670   bool may_wrap = false;
  9671   enum it_method prev_method = it->method;
  9672   ptrdiff_t closest_pos UNINIT;
  9673   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  9674   bool saw_smaller_pos = prev_pos < to_charpos;
  9675   bool line_number_pending = false;
  9676 
  9677   /* Don't produce glyphs in produce_glyphs.  */
  9678   saved_glyph_row = it->glyph_row;
  9679   it->glyph_row = NULL;
  9680 
  9681   /* Use wrap_it to save a copy of IT wherever a word wrap could
  9682      occur.  Use atpos_it to save a copy of IT at the desired buffer
  9683      position, if found, so that we can scan ahead and check if the
  9684      word later overshoots the window edge.  Use atx_it similarly, for
  9685      pixel positions.  */
  9686   wrap_it.sp = -1;
  9687   atpos_it.sp = -1;
  9688   atx_it.sp = -1;
  9689 
  9690   /* Use ppos_it under bidi reordering to save a copy of IT for the
  9691      initial position.  We restore that position in IT when we have
  9692      scanned the entire display line without finding a match for
  9693      TO_CHARPOS and all the character positions are greater than
  9694      TO_CHARPOS.  We then restart the scan from the initial position,
  9695      and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
  9696      the closest to TO_CHARPOS.  */
  9697   if (it->bidi_p)
  9698     {
  9699       if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
  9700         {
  9701           SAVE_IT (ppos_it, *it, ppos_data);
  9702           closest_pos = IT_CHARPOS (*it);
  9703         }
  9704       else
  9705         closest_pos = ZV;
  9706     }
  9707 
  9708 #define BUFFER_POS_REACHED_P()                                  \
  9709   ((op & MOVE_TO_POS) != 0                                      \
  9710    && BUFFERP (it->object)                                      \
  9711    && (IT_CHARPOS (*it) == to_charpos                           \
  9712        || ((!it->bidi_p                                         \
  9713             || BIDI_AT_BASE_LEVEL (it->bidi_it))                \
  9714            && IT_CHARPOS (*it) > to_charpos)                    \
  9715        || (it->what == IT_COMPOSITION                           \
  9716            && ((IT_CHARPOS (*it) > to_charpos                   \
  9717                 && to_charpos >= it->cmp_it.charpos)            \
  9718                || (IT_CHARPOS (*it) < to_charpos                \
  9719                    && to_charpos <= it->cmp_it.charpos))))      \
  9720    && (it->method == GET_FROM_BUFFER                            \
  9721        || (it->method == GET_FROM_DISPLAY_VECTOR                \
  9722            && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
  9723 
  9724   if (it->hpos == 0)
  9725     {
  9726       /* If line numbers are being displayed, produce a line number.
  9727          But don't do that if we are to reach first_visible_x, because
  9728          line numbers are not relevant to stuff that is not visible on
  9729          display.  */
  9730       if (!((op && MOVE_TO_X) && to_x == it->first_visible_x)
  9731           && should_produce_line_number (it))
  9732         {
  9733           if (it->current_x == it->first_visible_x)
  9734             maybe_produce_line_number (it);
  9735           else
  9736             line_number_pending = true;
  9737         }
  9738       /* If there's a line-/wrap-prefix, handle it, if we didn't already.  */
  9739       if (it->area == TEXT_AREA && !it->string_from_prefix_prop_p)
  9740         handle_line_prefix (it);
  9741     }
  9742 
  9743   if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9744     SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9745 
  9746   while (true)
  9747     {
  9748       int x, i, ascent = 0, descent = 0;
  9749 
  9750 /* Utility macro to reset an iterator with x, ascent, and descent.  */
  9751 #define IT_RESET_X_ASCENT_DESCENT(IT)                   \
  9752   ((IT)->current_x = x, (IT)->max_ascent = ascent,      \
  9753    (IT)->max_descent = descent)
  9754 
  9755       /* Stop if we move beyond TO_CHARPOS (after an image or a
  9756          display string or stretch glyph).  */
  9757       if ((op & MOVE_TO_POS) != 0
  9758           && BUFFERP (it->object)
  9759           && it->method == GET_FROM_BUFFER
  9760           && (((!it->bidi_p
  9761                 /* When the iterator is at base embedding level, we
  9762                    are guaranteed that characters are delivered for
  9763                    display in strictly increasing order of their
  9764                    buffer positions.  */
  9765                 || BIDI_AT_BASE_LEVEL (it->bidi_it))
  9766                && IT_CHARPOS (*it) > to_charpos)
  9767               || (it->bidi_p
  9768                   && (prev_method == GET_FROM_IMAGE
  9769                       || prev_method == GET_FROM_STRETCH
  9770                       || prev_method == GET_FROM_STRING)
  9771                   /* Passed TO_CHARPOS from left to right.  */
  9772                   && ((prev_pos < to_charpos
  9773                        && IT_CHARPOS (*it) >= to_charpos)
  9774                       /* Passed TO_CHARPOS from right to left.  */
  9775                       || (prev_pos > to_charpos
  9776                           && IT_CHARPOS (*it) <= to_charpos)))))
  9777         {
  9778           if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9779             {
  9780               result = MOVE_POS_MATCH_OR_ZV;
  9781               break;
  9782             }
  9783           else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
  9784             /* If wrap_it is valid, the current position might be in a
  9785                word that is wrapped.  So, save the iterator in
  9786                atpos_it and continue to see if wrapping happens.  */
  9787             SAVE_IT (atpos_it, *it, atpos_data);
  9788         }
  9789 
  9790       /* Stop when ZV reached.
  9791          We used to stop here when TO_CHARPOS reached as well, but that is
  9792          too soon if this glyph does not fit on this line.  So we handle it
  9793          explicitly below.  */
  9794       if (!get_next_display_element (it))
  9795         {
  9796           result = MOVE_POS_MATCH_OR_ZV;
  9797           break;
  9798         }
  9799 
  9800       if (it->line_wrap == TRUNCATE)
  9801         {
  9802           /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
  9803              produced something that doesn't consume any screen estate
  9804              in the text area, so we don't want to exit the loop at
  9805              TO_CHARPOS, before we produce the glyph for that buffer
  9806              position.  This happens, e.g., when there's an overlay at
  9807              TO_CHARPOS that draws a fringe bitmap.  */
  9808           if (BUFFER_POS_REACHED_P ()
  9809               && (it->pixel_width > 0
  9810                   || IT_CHARPOS (*it) > to_charpos
  9811                   || it->area != TEXT_AREA))
  9812             {
  9813               result = MOVE_POS_MATCH_OR_ZV;
  9814               break;
  9815             }
  9816         }
  9817       else
  9818         {
  9819           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
  9820             {
  9821               bool next_may_wrap = may_wrap;
  9822               /* Can we wrap after this character?  */
  9823               if (char_can_wrap_after (it))
  9824                 next_may_wrap = true;
  9825               else
  9826                 next_may_wrap = false;
  9827               /* Can we wrap here? */
  9828               if (may_wrap && char_can_wrap_before (it))
  9829                 {
  9830                   /* We have reached a glyph that follows one or more
  9831                      whitespace characters or characters that allow
  9832                      wrapping after them.  If this character allows
  9833                      wrapping before it, save this position as a
  9834                      wrapping point.  */
  9835                   if (atpos_it.sp >= 0)
  9836                     {
  9837                       RESTORE_IT (it, &atpos_it, atpos_data);
  9838                       result = MOVE_POS_MATCH_OR_ZV;
  9839                       goto done;
  9840                     }
  9841                   if (atx_it.sp >= 0)
  9842                     {
  9843                       RESTORE_IT (it, &atx_it, atx_data);
  9844                       result = MOVE_X_REACHED;
  9845                       goto done;
  9846                     }
  9847                   /* Otherwise, we can wrap here.  */
  9848                   SAVE_IT (wrap_it, *it, wrap_data);
  9849                 }
  9850               /* Update may_wrap for the next iteration.  */
  9851               may_wrap = next_may_wrap;
  9852             }
  9853         }
  9854 
  9855       /* Remember the line height for the current line, in case
  9856          the next element doesn't fit on the line.  */
  9857       ascent = it->max_ascent;
  9858       descent = it->max_descent;
  9859 
  9860       /* The call to produce_glyphs will get the metrics of the
  9861          display element IT is loaded with.  Record the x-position
  9862          before this display element, in case it doesn't fit on the
  9863          line.  */
  9864       x = it->current_x;
  9865 
  9866       PRODUCE_GLYPHS (it);
  9867 
  9868       if (it->area != TEXT_AREA)
  9869         {
  9870           prev_method = it->method;
  9871           if (it->method == GET_FROM_BUFFER)
  9872             prev_pos = IT_CHARPOS (*it);
  9873           set_iterator_to_next (it, true);
  9874           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9875             SET_TEXT_POS (this_line_min_pos,
  9876                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9877           if (it->bidi_p
  9878               && (op & MOVE_TO_POS)
  9879               && IT_CHARPOS (*it) > to_charpos
  9880               && IT_CHARPOS (*it) < closest_pos)
  9881             closest_pos = IT_CHARPOS (*it);
  9882           continue;
  9883         }
  9884 
  9885       /* The number of glyphs we get back in IT->nglyphs will normally
  9886          be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
  9887          character on a terminal frame, or (iii) a line end.  For the
  9888          second case, IT->nglyphs - 1 padding glyphs will be present.
  9889          (On X frames, there is only one glyph produced for a
  9890          composite character.)
  9891 
  9892          The behavior implemented below means, for continuation lines,
  9893          that as many spaces of a TAB as fit on the current line are
  9894          displayed there.  For terminal frames, as many glyphs of a
  9895          multi-glyph character are displayed in the current line, too.
  9896          This is what the old redisplay code did, and we keep it that
  9897          way.  Under X, the whole shape of a complex character must
  9898          fit on the line or it will be completely displayed in the
  9899          next line.
  9900 
  9901          Note that both for tabs and padding glyphs, all glyphs have
  9902          the same width.  */
  9903       if (it->nglyphs)
  9904         {
  9905           /* More than one glyph or glyph doesn't fit on line.  All
  9906              glyphs have the same width.  */
  9907           int single_glyph_width = it->pixel_width / it->nglyphs;
  9908           int new_x;
  9909           int x_before_this_char = x;
  9910           int hpos_before_this_char = it->hpos;
  9911 
  9912           for (i = 0; i < it->nglyphs; ++i, x = new_x)
  9913             {
  9914               new_x = x + single_glyph_width;
  9915 
  9916               /* We want to leave anything reaching TO_X to the caller.  */
  9917               if ((op & MOVE_TO_X) && new_x > to_x)
  9918                 {
  9919                   if (BUFFER_POS_REACHED_P ())
  9920                     {
  9921                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9922                         goto buffer_pos_reached;
  9923                       if (atpos_it.sp < 0)
  9924                         {
  9925                           SAVE_IT (atpos_it, *it, atpos_data);
  9926                           IT_RESET_X_ASCENT_DESCENT (&atpos_it);
  9927                         }
  9928                     }
  9929                   else
  9930                     {
  9931                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9932                         {
  9933                           it->current_x = x;
  9934                           result = MOVE_X_REACHED;
  9935                           break;
  9936                         }
  9937                       if (atx_it.sp < 0)
  9938                         {
  9939                           SAVE_IT (atx_it, *it, atx_data);
  9940                           IT_RESET_X_ASCENT_DESCENT (&atx_it);
  9941                         }
  9942                     }
  9943                 }
  9944 
  9945               if (/* Lines are continued.  */
  9946                   it->line_wrap != TRUNCATE
  9947                   && (/* And glyph doesn't fit on the line.  */
  9948                       new_x > it->last_visible_x
  9949                       /* Or it fits exactly and we're on a window
  9950                          system frame.  */
  9951                       || (new_x == it->last_visible_x
  9952                           && FRAME_WINDOW_P (it->f)
  9953                           && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  9954                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
  9955                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
  9956                 {
  9957                   bool moved_forward = false;
  9958 
  9959                   if (/* IT->hpos == 0 means the very first glyph
  9960                          doesn't fit on the line, e.g. a wide image.  */
  9961                       it->hpos == 0
  9962                       || (new_x == it->last_visible_x
  9963                           && FRAME_WINDOW_P (it->f)))
  9964                     {
  9965                       ++it->hpos;
  9966                       it->current_x = new_x;
  9967 
  9968                       /* The character's last glyph just barely fits
  9969                          in this row.  */
  9970                       if (i == it->nglyphs - 1)
  9971                         {
  9972                           /* If this is the destination position,
  9973                              return a position *before* it in this row,
  9974                              now that we know it fits in this row.  */
  9975                           if (BUFFER_POS_REACHED_P ())
  9976                             {
  9977                               bool can_wrap = true;
  9978 
  9979                               /* If the previous character says we can
  9980                                  wrap after it, but the current
  9981                                  character says we can't wrap before
  9982                                  it, then we can't wrap here.  */
  9983                               if (it->line_wrap == WORD_WRAP
  9984                                   && wrap_it.sp >= 0
  9985                                   && may_wrap
  9986                                   && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
  9987                                 {
  9988                                   struct it tem_it;
  9989                                   void *tem_data = NULL;
  9990 
  9991                                   SAVE_IT (tem_it, *it, tem_data);
  9992                                   set_iterator_to_next (it, true);
  9993                                   if (get_next_display_element (it)
  9994                                       && !char_can_wrap_before (it))
  9995                                     can_wrap = false;
  9996                                   RESTORE_IT (it, &tem_it, tem_data);
  9997                                 }
  9998                               if (it->line_wrap != WORD_WRAP
  9999                                   || wrap_it.sp < 0
 10000                                   /* If we've just found whitespace
 10001                                      where we can wrap, effectively
 10002                                      ignore the previous wrap point --
 10003                                      it is no longer relevant, but we
 10004                                      won't have an opportunity to
 10005                                      update it, since we've reached
 10006                                      the edge of this screen line.  */
 10007                                   || (may_wrap && can_wrap
 10008                                       && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10009                                 {
 10010                                   it->hpos = hpos_before_this_char;
 10011                                   it->current_x = x_before_this_char;
 10012                                   result = MOVE_POS_MATCH_OR_ZV;
 10013                                   break;
 10014                                 }
 10015                               if (it->line_wrap == WORD_WRAP
 10016                                   && atpos_it.sp < 0)
 10017                                 {
 10018                                   SAVE_IT (atpos_it, *it, atpos_data);
 10019                                   atpos_it.current_x = x_before_this_char;
 10020                                   atpos_it.hpos = hpos_before_this_char;
 10021                                 }
 10022                             }
 10023 
 10024                           prev_method = it->method;
 10025                           if (it->method == GET_FROM_BUFFER)
 10026                             prev_pos = IT_CHARPOS (*it);
 10027                           set_iterator_to_next (it, true);
 10028                           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10029                             SET_TEXT_POS (this_line_min_pos,
 10030                                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10031                           /* On graphical terminals, newlines may
 10032                              "overflow" into the fringe if
 10033                              overflow-newline-into-fringe is non-nil.
 10034                              On text terminals, and on graphical
 10035                              terminals with no right margin, newlines
 10036                              may overflow into the last glyph on the
 10037                              display line.*/
 10038                           if (!FRAME_WINDOW_P (it->f)
 10039                               || ((it->bidi_p
 10040                                    && it->bidi_it.paragraph_dir == R2L)
 10041                                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10042                                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10043                               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10044                             {
 10045                               if (!get_next_display_element (it))
 10046                                 {
 10047                                   result = MOVE_POS_MATCH_OR_ZV;
 10048                                   break;
 10049                                 }
 10050                               moved_forward = true;
 10051                               if (BUFFER_POS_REACHED_P ())
 10052                                 {
 10053                                   if (ITERATOR_AT_END_OF_LINE_P (it))
 10054                                     result = MOVE_POS_MATCH_OR_ZV;
 10055                                   else
 10056                                     result = MOVE_LINE_CONTINUED;
 10057                                   break;
 10058                                 }
 10059                               if (ITERATOR_AT_END_OF_LINE_P (it)
 10060                                   && (it->line_wrap != WORD_WRAP
 10061                                       || wrap_it.sp < 0
 10062                                       || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10063                                 {
 10064                                   result = MOVE_NEWLINE_OR_CR;
 10065                                   break;
 10066                                 }
 10067                             }
 10068                         }
 10069                     }
 10070                   else
 10071                     IT_RESET_X_ASCENT_DESCENT (it);
 10072 
 10073                   /* If the screen line ends with whitespace (or
 10074                      wrap-able character), and we are under word-wrap,
 10075                      don't use wrap_it: it is no longer relevant, but
 10076                      we won't have an opportunity to update it, since
 10077                      we are done with this screen line.  */
 10078                   if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 10079                       /* If the character after the one which set the
 10080                          may_wrap flag says we can't wrap before it,
 10081                          we can't wrap here.  Therefore, wrap_it
 10082                          (previously found wrap-point) _is_ relevant
 10083                          in that case.  */
 10084                       && (!moved_forward || char_can_wrap_before (it)))
 10085                     {
 10086                       /* If we've found TO_X, go back there, as we now
 10087                          know the last word fits on this screen line.  */
 10088                       if ((op & MOVE_TO_X) && new_x == it->last_visible_x
 10089                           && atx_it.sp >= 0)
 10090                         {
 10091                           RESTORE_IT (it, &atx_it, atx_data);
 10092                           atpos_it.sp = -1;
 10093                           atx_it.sp = -1;
 10094                           result = MOVE_X_REACHED;
 10095                           break;
 10096                         }
 10097                     }
 10098                   else if (wrap_it.sp >= 0)
 10099                     {
 10100                       RESTORE_IT (it, &wrap_it, wrap_data);
 10101                       atpos_it.sp = -1;
 10102                       atx_it.sp = -1;
 10103                     }
 10104 
 10105                   move_trace ("move_it_in: continued at %td\n",
 10106                               IT_CHARPOS (*it));
 10107                   result = MOVE_LINE_CONTINUED;
 10108                   break;
 10109                 }
 10110 
 10111               if (BUFFER_POS_REACHED_P ())
 10112                 {
 10113                   if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
 10114                     goto buffer_pos_reached;
 10115                   if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
 10116                     {
 10117                       SAVE_IT (atpos_it, *it, atpos_data);
 10118                       IT_RESET_X_ASCENT_DESCENT (&atpos_it);
 10119                     }
 10120                 }
 10121 
 10122               if (new_x > it->first_visible_x)
 10123                 {
 10124                   /* If we have reached the visible portion of the
 10125                      screen line, produce the line number if needed.  */
 10126                   if (line_number_pending)
 10127                     {
 10128                       line_number_pending = false;
 10129                       it->current_x = it->first_visible_x;
 10130                       maybe_produce_line_number (it);
 10131                       it->current_x += new_x - it->first_visible_x;
 10132                     }
 10133                   /* Glyph is visible.  Increment number of glyphs that
 10134                      would be displayed.  */
 10135                   ++it->hpos;
 10136                 }
 10137             }
 10138 
 10139           if (result != MOVE_UNDEFINED)
 10140             break;
 10141         }
 10142       else if (BUFFER_POS_REACHED_P ())
 10143         {
 10144         buffer_pos_reached:
 10145           IT_RESET_X_ASCENT_DESCENT (it);
 10146           result = MOVE_POS_MATCH_OR_ZV;
 10147           break;
 10148         }
 10149       else if ((op & MOVE_TO_X) && it->current_x >= to_x)
 10150         {
 10151           /* Stop when TO_X specified and reached.  This check is
 10152              necessary here because of lines consisting of a line end,
 10153              only.  The line end will not produce any glyphs and we
 10154              would never get MOVE_X_REACHED.  */
 10155           eassert (it->nglyphs == 0);
 10156           result = MOVE_X_REACHED;
 10157           break;
 10158         }
 10159 
 10160       /* Is this a line end?  If yes, we're done.  */
 10161       if (ITERATOR_AT_END_OF_LINE_P (it))
 10162         {
 10163           /* If we are past TO_CHARPOS, but never saw any character
 10164              positions smaller than TO_CHARPOS, return
 10165              MOVE_POS_MATCH_OR_ZV, like the unidirectional display
 10166              did.  */
 10167           if (it->bidi_p && (op & MOVE_TO_POS) != 0)
 10168             {
 10169               if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
 10170                 {
 10171                   if (closest_pos < ZV)
 10172                     {
 10173                       RESTORE_IT (it, &ppos_it, ppos_data);
 10174                       /* Don't recurse if closest_pos is equal to
 10175                          to_charpos, since we have just tried that.  */
 10176                       if (closest_pos != to_charpos)
 10177                         move_it_in_display_line_to (it, closest_pos, -1,
 10178                                                     MOVE_TO_POS);
 10179                       result = MOVE_POS_MATCH_OR_ZV;
 10180                     }
 10181                   else
 10182                     goto buffer_pos_reached;
 10183                 }
 10184               else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
 10185                        && IT_CHARPOS (*it) > to_charpos)
 10186                 goto buffer_pos_reached;
 10187               else
 10188                 result = MOVE_NEWLINE_OR_CR;
 10189             }
 10190           else
 10191             result = MOVE_NEWLINE_OR_CR;
 10192           /* If lines are truncated, and the line we moved across is
 10193              completely hscrolled out of view, reset the line metrics
 10194              to those of the newline we've just processed, so that
 10195              glyphs not on display don't affect the line's height.  */
 10196           if (it->line_wrap == TRUNCATE
 10197               && it->current_x <= it->first_visible_x
 10198               && result == MOVE_NEWLINE_OR_CR
 10199               && it->char_to_display == '\n')
 10200             {
 10201               it->max_ascent = it->ascent;
 10202               it->max_descent = it->descent;
 10203             }
 10204           /* If we've processed the newline, make sure this flag is
 10205              reset, as it must only be set when the newline itself is
 10206              processed.  */
 10207           if (result == MOVE_NEWLINE_OR_CR)
 10208             it->constrain_row_ascent_descent_p = false;
 10209           break;
 10210         }
 10211 
 10212       prev_method = it->method;
 10213       if (it->method == GET_FROM_BUFFER)
 10214         prev_pos = IT_CHARPOS (*it);
 10215 
 10216       /* Detect overly-wide wrap-prefixes made of (space ...) display
 10217          properties.  When such a wrap prefix reaches past the right
 10218          margin of the window, we need to avoid the call to
 10219          set_iterator_to_next below, so that it->line_wrap is left at
 10220          its TRUNCATE value wisely set by handle_line_prefix.
 10221          Otherwise, set_iterator_to_next will pop the iterator stack,
 10222          restore it->line_wrap, and we might miss the opportunity to
 10223          exit the loop and return.  */
 10224       bool overwide_wrap_prefix =
 10225         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 10226         && it->sp > 0 && it->method == GET_FROM_STRETCH
 10227         && it->current_x >= it->last_visible_x
 10228         && it->continuation_lines_width > 0
 10229         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 10230       /* The current display element has been consumed.  Advance
 10231          to the next.  */
 10232       if (!overwide_wrap_prefix)
 10233         set_iterator_to_next (it, true);
 10234       if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10235         SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10236       if (IT_CHARPOS (*it) < to_charpos)
 10237         saw_smaller_pos = true;
 10238       if (it->bidi_p
 10239           && (op & MOVE_TO_POS)
 10240           && IT_CHARPOS (*it) >= to_charpos
 10241           && IT_CHARPOS (*it) < closest_pos)
 10242         closest_pos = IT_CHARPOS (*it);
 10243 
 10244       /* Stop if lines are truncated and IT's current x-position is
 10245          past the right edge of the window now.  */
 10246       if (it->line_wrap == TRUNCATE
 10247           && it->current_x >= it->last_visible_x)
 10248         {
 10249           if (!FRAME_WINDOW_P (it->f)
 10250               || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10251                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10252                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10253               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10254             {
 10255               bool at_eob_p = false;
 10256 
 10257               if ((at_eob_p = !get_next_display_element (it))
 10258                   || BUFFER_POS_REACHED_P ()
 10259                   /* If we are past TO_CHARPOS, but never saw any
 10260                      character positions smaller than TO_CHARPOS,
 10261                      return MOVE_POS_MATCH_OR_ZV, like the
 10262                      unidirectional display did.  */
 10263                   || (it->bidi_p && (op & MOVE_TO_POS) != 0
 10264                       && !saw_smaller_pos
 10265                       && IT_CHARPOS (*it) > to_charpos))
 10266                 {
 10267                   if (it->bidi_p
 10268                       && !BUFFER_POS_REACHED_P ()
 10269                       && !at_eob_p && closest_pos < ZV)
 10270                     {
 10271                       RESTORE_IT (it, &ppos_it, ppos_data);
 10272                       if (closest_pos != to_charpos)
 10273                         move_it_in_display_line_to (it, closest_pos, -1,
 10274                                                     MOVE_TO_POS);
 10275                     }
 10276                   result = MOVE_POS_MATCH_OR_ZV;
 10277                   break;
 10278                 }
 10279               if (ITERATOR_AT_END_OF_LINE_P (it))
 10280                 {
 10281                   result = MOVE_NEWLINE_OR_CR;
 10282                   break;
 10283                 }
 10284             }
 10285           else if (it->bidi_p && (op & MOVE_TO_POS) != 0
 10286                    && !saw_smaller_pos
 10287                    && IT_CHARPOS (*it) > to_charpos)
 10288             {
 10289               if (closest_pos < ZV)
 10290                 {
 10291                   RESTORE_IT (it, &ppos_it, ppos_data);
 10292                   if (closest_pos != to_charpos)
 10293                     move_it_in_display_line_to (it, closest_pos, -1,
 10294                                                 MOVE_TO_POS);
 10295                 }
 10296               result = MOVE_POS_MATCH_OR_ZV;
 10297               break;
 10298             }
 10299           result = MOVE_LINE_TRUNCATED;
 10300           break;
 10301         }
 10302 #undef IT_RESET_X_ASCENT_DESCENT
 10303     }
 10304 
 10305 #undef BUFFER_POS_REACHED_P
 10306 
 10307   /* If we scanned beyond TO_POS, restore the saved iterator either to
 10308      the wrap point (if found), or to atpos/atx location.  We decide which
 10309      data to use to restore the saved iterator state by their X coordinates,
 10310      since buffer positions might increase non-monotonically with screen
 10311      coordinates due to bidi reordering.  */
 10312   if (result == MOVE_LINE_CONTINUED
 10313       && it->line_wrap == WORD_WRAP
 10314       && wrap_it.sp >= 0
 10315       && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
 10316           || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
 10317     RESTORE_IT (it, &wrap_it, wrap_data);
 10318   else if (atpos_it.sp >= 0)
 10319     RESTORE_IT (it, &atpos_it, atpos_data);
 10320   else if (atx_it.sp >= 0)
 10321     RESTORE_IT (it, &atx_it, atx_data);
 10322 
 10323  done:
 10324 
 10325   if (atpos_data)
 10326     bidi_unshelve_cache (atpos_data, true);
 10327   if (atx_data)
 10328     bidi_unshelve_cache (atx_data, true);
 10329   if (wrap_data)
 10330     bidi_unshelve_cache (wrap_data, true);
 10331   if (ppos_data)
 10332     bidi_unshelve_cache (ppos_data, true);
 10333 
 10334   /* Restore the iterator settings altered at the beginning of this
 10335      function.  */
 10336   it->glyph_row = saved_glyph_row;
 10337   return result;
 10338 }
 10339 
 10340 /* For external use.  */
 10341 void
 10342 move_it_in_display_line (struct it *it,
 10343                          ptrdiff_t to_charpos, int to_x,
 10344                          enum move_operation_enum op)
 10345 {
 10346   if (it->line_wrap == WORD_WRAP
 10347       && (op & MOVE_TO_X))
 10348     {
 10349       struct it save_it;
 10350       void *save_data = NULL;
 10351       int skip;
 10352 
 10353       SAVE_IT (save_it, *it, save_data);
 10354       skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10355       /* When word-wrap is on, TO_X may lie past the end
 10356          of a wrapped line.  Then it->current is the
 10357          character on the next line, so backtrack to the
 10358          space before the wrap point.  */
 10359       if (skip == MOVE_LINE_CONTINUED)
 10360         {
 10361           int prev_x = max (it->current_x - 1, 0);
 10362           RESTORE_IT (it, &save_it, save_data);
 10363           move_it_in_display_line_to
 10364             (it, -1, prev_x, MOVE_TO_X);
 10365         }
 10366       else
 10367         bidi_unshelve_cache (save_data, true);
 10368     }
 10369   else
 10370     move_it_in_display_line_to (it, to_charpos, to_x, op);
 10371 }
 10372 
 10373 
 10374 /* Move IT forward until it satisfies one or more of the criteria in
 10375    TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
 10376 
 10377    OP is a bit-mask that specifies where to stop, and in particular,
 10378    which of those four position arguments makes a difference.  See the
 10379    description of enum move_operation_enum.
 10380 
 10381    If TO_CHARPOS is in invisible text, e.g. a truncated part of a
 10382    screen line, this function will set IT to the next position that is
 10383    displayed to the right of TO_CHARPOS on the screen.
 10384 
 10385    Return the maximum pixel length of any line scanned but never more
 10386    than it.last_visible_x.  */
 10387 
 10388 int
 10389 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
 10390 {
 10391   enum move_it_result skip, skip2 = MOVE_X_REACHED;
 10392   int line_height, line_start_x = 0, reached = 0;
 10393   int max_current_x = 0;
 10394   void *backup_data = NULL;
 10395   ptrdiff_t orig_charpos = -1;
 10396   enum it_method orig_method = NUM_IT_METHODS;
 10397 
 10398   for (;;)
 10399     {
 10400       orig_charpos = IT_CHARPOS (*it);
 10401       orig_method = it->method;
 10402       if (op & MOVE_TO_VPOS)
 10403         {
 10404           /* If no TO_CHARPOS and no TO_X specified, stop at the
 10405              start of the line TO_VPOS.  */
 10406           if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
 10407             {
 10408               if (it->vpos == to_vpos)
 10409                 {
 10410                   reached = 1;
 10411                   break;
 10412                 }
 10413               else
 10414                 skip = move_it_in_display_line_to (it, -1, -1, 0);
 10415             }
 10416           else
 10417             {
 10418               /* TO_VPOS >= 0 means stop at TO_X in the line at
 10419                  TO_VPOS, or at TO_POS, whichever comes first.  */
 10420               if (it->vpos == to_vpos)
 10421                 {
 10422                   reached = 2;
 10423                   break;
 10424                 }
 10425 
 10426               skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10427 
 10428               if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
 10429                 {
 10430                   reached = 3;
 10431                   break;
 10432                 }
 10433               else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
 10434                 {
 10435                   /* We have reached TO_X but not in the line we want.  */
 10436                   skip = move_it_in_display_line_to (it, to_charpos,
 10437                                                      -1, MOVE_TO_POS);
 10438                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10439                     {
 10440                       reached = 4;
 10441                       break;
 10442                     }
 10443                 }
 10444             }
 10445         }
 10446       else if (op & MOVE_TO_Y)
 10447         {
 10448           struct it it_backup;
 10449 
 10450           if (it->line_wrap == WORD_WRAP)
 10451             SAVE_IT (it_backup, *it, backup_data);
 10452 
 10453           /* TO_Y specified means stop at TO_X in the line containing
 10454              TO_Y---or at TO_CHARPOS if this is reached first.  The
 10455              problem is that we can't really tell whether the line
 10456              contains TO_Y before we have completely scanned it, and
 10457              this may skip past TO_X.  What we do is to first scan to
 10458              TO_X.
 10459 
 10460              If TO_X is not specified, use a TO_X of zero.  The reason
 10461              is to make the outcome of this function more predictable.
 10462              If we didn't use TO_X == 0, we would stop at the end of
 10463              the line which is probably not what a caller would expect
 10464              to happen.  */
 10465           skip = move_it_in_display_line_to
 10466             (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
 10467              (MOVE_TO_X | (op & MOVE_TO_POS)));
 10468 
 10469           /* If TO_CHARPOS is reached or ZV, we don't have to do more.  */
 10470           if (skip == MOVE_POS_MATCH_OR_ZV)
 10471             reached = 5;
 10472           else if (skip == MOVE_X_REACHED)
 10473             {
 10474               /* If TO_X was reached, we want to know whether TO_Y is
 10475                  in the line.  We know this is the case if the already
 10476                  scanned glyphs make the line tall enough.  Otherwise,
 10477                  we must check by scanning the rest of the line.  */
 10478               line_height = it->max_ascent + it->max_descent;
 10479               if (to_y >= it->current_y
 10480                   && to_y < it->current_y + line_height)
 10481                 {
 10482                   reached = 6;
 10483                   break;
 10484                 }
 10485               SAVE_IT (it_backup, *it, backup_data);
 10486               move_trace ("move_it: from %td\n", IT_CHARPOS (*it));
 10487               skip2 = move_it_in_display_line_to (it, to_charpos, -1,
 10488                                                   op & MOVE_TO_POS);
 10489               move_trace ("move_it: to %td\n", IT_CHARPOS (*it));
 10490               line_height = it->max_ascent + it->max_descent;
 10491               move_trace ("move_it: line_height = %d\n", line_height);
 10492 
 10493               if (to_y >= it->current_y
 10494                   && to_y < it->current_y + line_height)
 10495                 {
 10496                   /* If TO_Y is in this line and TO_X was reached
 10497                      above, we scanned too far.  We have to restore
 10498                      IT's settings to the ones before skipping.  But
 10499                      keep the more accurate values of max_ascent and
 10500                      max_descent we've found while skipping the rest
 10501                      of the line, for the sake of callers, such as
 10502                      pos_visible_p, that need to know the line
 10503                      height.  */
 10504                   int max_ascent = it->max_ascent;
 10505                   int max_descent = it->max_descent;
 10506 
 10507                   RESTORE_IT (it, &it_backup, backup_data);
 10508                   it->max_ascent = max_ascent;
 10509                   it->max_descent = max_descent;
 10510                   reached = 6;
 10511                 }
 10512               else
 10513                 {
 10514                   skip = skip2;
 10515                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10516                     {
 10517                       reached = 7;
 10518                       /* If the last move_it_in_display_line_to call
 10519                          took us away from TO_CHARPOS, back up to the
 10520                          previous position, as it is a better
 10521                          approximation of TO_CHARPOS.  (Note that we
 10522                          could have both positions after TO_CHARPOS or
 10523                          both positions before it, due to bidi
 10524                          reordering.)  */
 10525                       if (to_charpos > 0
 10526                           && IT_CHARPOS (*it) != to_charpos
 10527                           && ((IT_CHARPOS (it_backup) > to_charpos)
 10528                               == (IT_CHARPOS (*it) > to_charpos)))
 10529                         {
 10530                           int max_ascent = it->max_ascent;
 10531                           int max_descent = it->max_descent;
 10532 
 10533                           RESTORE_IT (it, &it_backup, backup_data);
 10534                           it->max_ascent = max_ascent;
 10535                           it->max_descent = max_descent;
 10536                         }
 10537                     }
 10538                 }
 10539             }
 10540           else
 10541             {
 10542               /* Check whether TO_Y is in this line.  */
 10543               line_height = it->max_ascent + it->max_descent;
 10544               move_trace ("move_it: line_height = %d\n", line_height);
 10545 
 10546               if (to_y >= it->current_y
 10547                   && to_y < it->current_y + line_height)
 10548                 {
 10549                   if (to_y > it->current_y)
 10550                     max_current_x = max (it->current_x, max_current_x);
 10551 
 10552                   /* When word-wrap is on, TO_X may lie past the end
 10553                      of a wrapped line.  Then it->current is the
 10554                      character on the next line, so backtrack to the
 10555                      space before the wrap point.  */
 10556                   if (skip == MOVE_LINE_CONTINUED
 10557                       && it->line_wrap == WORD_WRAP)
 10558                     {
 10559                       int prev_x = max (it->current_x - 1, 0);
 10560                       RESTORE_IT (it, &it_backup, backup_data);
 10561                       skip = move_it_in_display_line_to
 10562                         (it, -1, prev_x, MOVE_TO_X);
 10563                     }
 10564 
 10565                   reached = 6;
 10566                 }
 10567             }
 10568 
 10569           if (reached)
 10570             {
 10571               max_current_x = max (it->current_x, max_current_x);
 10572               break;
 10573             }
 10574         }
 10575       else if (BUFFERP (it->object)
 10576                && (it->method == GET_FROM_BUFFER
 10577                    || it->method == GET_FROM_STRETCH)
 10578                && IT_CHARPOS (*it) >= to_charpos
 10579                /* Under bidi iteration, a call to set_iterator_to_next
 10580                   can scan far beyond to_charpos if the initial
 10581                   portion of the next line needs to be reordered.  In
 10582                   that case, give move_it_in_display_line_to another
 10583                   chance below.  */
 10584                && !(it->bidi_p
 10585                     && it->bidi_it.scan_dir == -1))
 10586         skip = MOVE_POS_MATCH_OR_ZV;
 10587       else
 10588         skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
 10589 
 10590       switch (skip)
 10591         {
 10592         case MOVE_POS_MATCH_OR_ZV:
 10593           max_current_x = max (it->current_x, max_current_x);
 10594           reached = 8;
 10595           goto out;
 10596 
 10597         case MOVE_NEWLINE_OR_CR:
 10598           max_current_x = max (it->current_x, max_current_x);
 10599           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10600             it->override_ascent = -1;
 10601           set_iterator_to_next (it, true);
 10602           it->continuation_lines_width = 0;
 10603           break;
 10604 
 10605         case MOVE_LINE_TRUNCATED:
 10606           max_current_x = it->last_visible_x;
 10607           it->continuation_lines_width = 0;
 10608           reseat_at_next_visible_line_start (it, false);
 10609           if ((op & MOVE_TO_POS) != 0
 10610               && (IT_CHARPOS (*it) > to_charpos
 10611                   || (IT_CHARPOS (*it) == to_charpos
 10612                       /* Consider TO_CHARPOS as REACHED if we are at
 10613                          EOB that ends in something other than a newline.  */
 10614                       && to_charpos == ZV
 10615                       && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n')
 10616                       /* But if we have a display or an overlay string
 10617                          at EOB, keep going until we exhaust all the
 10618                          characters of the string(s).  */
 10619                       && (it->sp == 0
 10620                           || (STRINGP (it->string)
 10621                               && (it->current.overlay_string_index < 0
 10622                                   || (it->current.overlay_string_index >= 0
 10623                                       && it->current.overlay_string_index
 10624                                          >= it->n_overlay_strings - 1))
 10625                               && IT_STRING_CHARPOS (*it) >= it->end_charpos)))))
 10626             {
 10627               reached = 9;
 10628               goto out;
 10629             }
 10630           break;
 10631 
 10632         case MOVE_LINE_CONTINUED:
 10633           max_current_x = it->last_visible_x;
 10634           /* For continued lines ending in a tab, some of the glyphs
 10635              associated with the tab are displayed on the current
 10636              line.  Since it->current_x does not include these glyphs,
 10637              we use it->last_visible_x instead.  */
 10638           if (it->c == '\t')
 10639             {
 10640               it->continuation_lines_width += it->last_visible_x;
 10641               /* When moving by vpos, ensure that the iterator really
 10642                  advances to the next line (bug#847, bug#969).  Fixme:
 10643                  do we need to do this in other circumstances?  */
 10644               if (it->current_x != it->last_visible_x
 10645                   && (op & MOVE_TO_VPOS)
 10646                   && !(op & (MOVE_TO_X | MOVE_TO_POS)))
 10647                 {
 10648                   line_start_x = it->current_x + it->pixel_width
 10649                     - it->last_visible_x;
 10650                   if (FRAME_WINDOW_P (it->f))
 10651                     {
 10652                       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 10653                       struct font *face_font = face->font;
 10654 
 10655                       /* When display_line produces a continued line
 10656                          that ends in a TAB, it skips a tab stop that
 10657                          is closer than the font's space character
 10658                          width (see gui_produce_glyphs where it produces
 10659                          the stretch glyph which represents a TAB).
 10660                          We need to reproduce the same logic here.  */
 10661                       eassert (face_font);
 10662                       if (face_font)
 10663                         {
 10664                           if (line_start_x < face_font->space_width)
 10665                             line_start_x
 10666                               += it->tab_width * face_font->space_width;
 10667                         }
 10668                     }
 10669                   set_iterator_to_next (it, false);
 10670                 }
 10671             }
 10672           else
 10673             {
 10674               /* Make sure we do advance, otherwise we might infloop.
 10675                  This could happen when the first display element is
 10676                  wider than the window, or if we have a wrap-prefix
 10677                  that doesn't leave enough space after it to display
 10678                  even a single character.  We only do this for moving
 10679                  through buffer text, as with display/overlay strings
 10680                  we'd need to also compare it->object's, and this is
 10681                  unlikely to happen in that case anyway.  */
 10682               if (IT_CHARPOS (*it) == orig_charpos
 10683                   && it->method == orig_method
 10684                   && orig_method == GET_FROM_BUFFER)
 10685                 set_iterator_to_next (it, false);
 10686               it->continuation_lines_width += it->current_x;
 10687             }
 10688           break;
 10689 
 10690         default:
 10691           emacs_abort ();
 10692         }
 10693 
 10694       /* Reset/increment for the next run.  */
 10695       it->current_x = line_start_x;
 10696       line_start_x = 0;
 10697       it->hpos = 0;
 10698       it->line_number_produced_p = false;
 10699       it->current_y += it->max_ascent + it->max_descent;
 10700       ++it->vpos;
 10701       last_height = it->max_ascent + it->max_descent;
 10702       it->max_ascent = it->max_descent = 0;
 10703     }
 10704 
 10705  out:
 10706 
 10707   /* On text terminals, we may stop at the end of a line in the middle
 10708      of a multi-character glyph.  If the glyph itself is continued,
 10709      i.e. it is actually displayed on the next line, don't treat this
 10710      stopping point as valid; move to the next line instead (unless
 10711      that brings us offscreen).  */
 10712   if (!FRAME_WINDOW_P (it->f)
 10713       && op & MOVE_TO_POS
 10714       && IT_CHARPOS (*it) == to_charpos
 10715       && it->what == IT_CHARACTER
 10716       && it->nglyphs > 1
 10717       && it->line_wrap == WINDOW_WRAP
 10718       && it->current_x == it->last_visible_x - 1
 10719       && it->c != '\n'
 10720       && it->c != '\t'
 10721       && it->w->window_end_valid
 10722       && it->vpos < it->w->window_end_vpos)
 10723     {
 10724       it->continuation_lines_width += it->current_x;
 10725       it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
 10726       it->current_y += it->max_ascent + it->max_descent;
 10727       ++it->vpos;
 10728       last_height = it->max_ascent + it->max_descent;
 10729     }
 10730 
 10731   if (backup_data)
 10732     bidi_unshelve_cache (backup_data, true);
 10733 
 10734   move_trace ("move_it_to: reached %d\n", reached);
 10735 
 10736   return max_current_x;
 10737 }
 10738 
 10739 
 10740 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
 10741 
 10742    If DY > 0, move IT backward that many pixels.
 10743    DY = 0 means move IT backward to the preceding line start or to BEGV.
 10744    This function may move over less or more than DY pixels if
 10745    IT->current_y - DY ends up in the middle of a line; in this case
 10746    IT->current_y will be set to the top of the line either before or
 10747    after the exact pixel coordinate.  */
 10748 
 10749 void
 10750 move_it_vertically_backward (struct it *it, int dy)
 10751 {
 10752   int nlines, h;
 10753   struct it it2, it3;
 10754   void *it2data = NULL, *it3data = NULL;
 10755   ptrdiff_t start_pos;
 10756   int nchars_per_row
 10757     = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 10758   ptrdiff_t pos_limit;
 10759 
 10760  move_further_back:
 10761   eassert (dy >= 0);
 10762 
 10763   start_pos = IT_CHARPOS (*it);
 10764 
 10765   /* Estimate how many newlines we must move back.  */
 10766   nlines = max (1, dy / default_line_pixel_height (it->w));
 10767   if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 10768     pos_limit = BEGV;
 10769   else
 10770     pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
 10771 
 10772   /* Set the iterator's position that many lines back.  But don't go
 10773      back more than NLINES full screen lines -- this wins a day with
 10774      buffers which have very long lines.  */
 10775   while (nlines-- && IT_CHARPOS (*it) > pos_limit)
 10776     back_to_previous_visible_line_start (it);
 10777 
 10778   /* Reseat the iterator here.  When moving backward, we don't want
 10779      reseat to skip forward over invisible text, set up the iterator
 10780      to deliver from overlay strings at the new position etc.  So,
 10781      use reseat_1 here.  */
 10782   reseat_1 (it, it->current.pos, true);
 10783 
 10784   /* We are now surely at a line start.  */
 10785   it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
 10786                                    reordering is in effect.  */
 10787   it->continuation_lines_width = 0;
 10788 
 10789   /* Move forward and see what y-distance we moved.  First move to the
 10790      start of the next line so that we get its height.  We need this
 10791      height to be able to tell whether we reached the specified
 10792      y-distance.  */
 10793   SAVE_IT (it2, *it, it2data);
 10794   it2.max_ascent = it2.max_descent = 0;
 10795   do
 10796     {
 10797       move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
 10798                   MOVE_TO_POS | MOVE_TO_VPOS);
 10799     }
 10800   while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
 10801            /* If we are in a display string which starts at START_POS,
 10802               and that display string includes a newline, and we are
 10803               right after that newline (i.e. at the beginning of a
 10804               display line), exit the loop, because otherwise we will
 10805               infloop, since move_it_to will see that it is already at
 10806               START_POS and will not move.  */
 10807            || (it2.method == GET_FROM_STRING
 10808                && IT_CHARPOS (it2) == start_pos
 10809                && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
 10810   eassert (IT_CHARPOS (*it) >= BEGV);
 10811   SAVE_IT (it3, it2, it3data);
 10812 
 10813   move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
 10814   eassert (IT_CHARPOS (*it) >= BEGV);
 10815   /* H is the actual vertical distance from the position in *IT
 10816      and the starting position.  */
 10817   h = it2.current_y - it->current_y;
 10818   /* NLINES is the distance in number of lines.  */
 10819   nlines = it2.vpos - it->vpos;
 10820 
 10821   /* Correct IT's y and vpos position
 10822      so that they are relative to the starting point.  */
 10823   it->vpos -= nlines;
 10824   it->current_y -= h;
 10825 
 10826   if (dy == 0)
 10827     {
 10828       /* DY == 0 means move to the start of the screen line.  The
 10829          value of nlines is > 0 if continuation lines were involved,
 10830          or if the original IT position was at start of a line.  */
 10831       RESTORE_IT (it, it, it2data);
 10832       if (nlines > 0)
 10833         move_it_by_lines (it, nlines);
 10834       /* The above code moves us to some position NLINES down,
 10835          usually to its first glyph (leftmost in an L2R line), but
 10836          that's not necessarily the start of the line, under bidi
 10837          reordering.  We want to get to the character position
 10838          that is immediately after the newline of the previous
 10839          line.  */
 10840       if (it->bidi_p
 10841           && !it->continuation_lines_width
 10842           && !STRINGP (it->string)
 10843           && IT_CHARPOS (*it) > BEGV
 10844           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10845         {
 10846           ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
 10847 
 10848           dec_both (&cp, &bp);
 10849           SET_WITH_NARROWED_BEGV (it, cp,
 10850                                   find_newline_no_quit (cp, bp, -1, NULL),
 10851                                   get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
 10852           move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
 10853         }
 10854       bidi_unshelve_cache (it3data, true);
 10855     }
 10856   else
 10857     {
 10858       /* The y-position we try to reach, relative to *IT.
 10859          Note that H has been subtracted in front of the if-statement.  */
 10860       int target_y = it->current_y + h - dy;
 10861       int y0 = it3.current_y;
 10862       int y1;
 10863       int line_height;
 10864 
 10865       RESTORE_IT (&it3, &it3, it3data);
 10866       y1 = line_bottom_y (&it3);
 10867       line_height = y1 - y0;
 10868       RESTORE_IT (it, it, it2data);
 10869       /* If we did not reach target_y, try to move further backward if
 10870          we can.  If we moved too far backward, try to move forward.  */
 10871       if (target_y < it->current_y
 10872           /* This is heuristic.  In a window that's 3 lines high, with
 10873              a line height of 13 pixels each, recentering with point
 10874              on the bottom line will try to move -39/2 = 19 pixels
 10875              backward.  Try to avoid moving into the first line.  */
 10876           && (it->current_y - target_y
 10877               > min (window_box_height (it->w), line_height * 2 / 3))
 10878           && IT_CHARPOS (*it) > BEGV)
 10879         {
 10880           move_trace ("  not far enough -> move_vert %d\n",
 10881                       target_y - it->current_y);
 10882           dy = it->current_y - target_y;
 10883           goto move_further_back;
 10884         }
 10885       else if (target_y >= it->current_y + line_height
 10886                && IT_CHARPOS (*it) < ZV)
 10887         {
 10888           /* Should move forward by at least one line, maybe more.
 10889 
 10890              Note: Calling move_it_by_lines can be expensive on
 10891              terminal frames, where compute_motion is used (via
 10892              vmotion) to do the job, when there are very long lines
 10893              and truncate-lines is nil.  That's the reason for
 10894              treating terminal frames specially here.  */
 10895 
 10896           if (!FRAME_WINDOW_P (it->f))
 10897             move_it_vertically (it, target_y - it->current_y);
 10898           else
 10899             {
 10900               struct text_pos last_pos;
 10901               int last_y, last_vpos;
 10902               do
 10903                 {
 10904                   last_pos = it->current.pos;
 10905                   last_y = it->current_y;
 10906                   last_vpos = it->vpos;
 10907                   move_it_by_lines (it, 1);
 10908                 }
 10909               while (target_y > it->current_y && IT_CHARPOS (*it) < ZV);
 10910               if (it->current_y > target_y)
 10911                 {
 10912                   reseat (it, last_pos, true);
 10913                   it->current_y = last_y;
 10914                   it->vpos = last_vpos;
 10915                 }
 10916             }
 10917         }
 10918     }
 10919 }
 10920 
 10921 
 10922 /* Move IT by a specified amount of pixel lines DY.  DY negative means
 10923    move backwards.  DY = 0 means move to start of screen line.  At the
 10924    end, IT will be on the start of a screen line.  */
 10925 
 10926 void
 10927 move_it_vertically (struct it *it, int dy)
 10928 {
 10929   if (dy <= 0)
 10930     move_it_vertically_backward (it, -dy);
 10931   else
 10932     {
 10933       move_trace ("move_it_v: from %td, %d\n", IT_CHARPOS (*it), dy);
 10934       move_it_to (it, ZV, -1, it->current_y + dy, -1,
 10935                   MOVE_TO_POS | MOVE_TO_Y);
 10936       move_trace ("move_it_v: to %td\n", IT_CHARPOS (*it));
 10937 
 10938       /* If buffer ends in ZV without a newline, move to the start of
 10939          the line to satisfy the post-condition.  */
 10940       if (IT_CHARPOS (*it) == ZV
 10941           && ZV > BEGV
 10942           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10943         move_it_by_lines (it, 0);
 10944     }
 10945 }
 10946 
 10947 
 10948 /* Move iterator IT past the end of the text line it is in.  */
 10949 
 10950 void
 10951 move_it_past_eol (struct it *it)
 10952 {
 10953   enum move_it_result rc;
 10954 
 10955   rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
 10956   if (rc == MOVE_NEWLINE_OR_CR)
 10957     set_iterator_to_next (it, false);
 10958 }
 10959 
 10960 
 10961 /* Move IT by a specified number DVPOS of screen lines down.  DVPOS
 10962    negative means move up.  DVPOS == 0 means move to the start of the
 10963    screen line.
 10964 
 10965    Optimization idea: If we would know that IT->f doesn't use
 10966    a face with proportional font, we could be faster for
 10967    truncate-lines nil.  */
 10968 
 10969 void
 10970 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
 10971 {
 10972 
 10973   /* The commented-out optimization uses vmotion on terminals.  This
 10974      gives bad results, because elements like it->what, on which
 10975      callers such as pos_visible_p rely, aren't updated.  */
 10976   /* struct position pos;
 10977     if (!FRAME_WINDOW_P (it->f))
 10978     {
 10979       struct text_pos textpos;
 10980 
 10981       pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
 10982       SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
 10983       reseat (it, textpos, true);
 10984       it->vpos += pos.vpos;
 10985       it->current_y += pos.vpos;
 10986     }
 10987     else */
 10988 
 10989   if (dvpos == 0)
 10990     {
 10991       /* DVPOS == 0 means move to the start of the screen line.  */
 10992       move_it_vertically_backward (it, 0);
 10993       /* Let next call to line_bottom_y calculate real line height.  */
 10994       last_height = 0;
 10995     }
 10996   else if (dvpos > 0)
 10997     {
 10998       move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
 10999       if (!IT_POS_VALID_AFTER_MOVE_P (it))
 11000         {
 11001           /* Only move to the next buffer position if we ended up in a
 11002              string from display property, not in an overlay string
 11003              (before-string or after-string).  That is because the
 11004              latter don't conceal the underlying buffer position, so
 11005              we can ask to move the iterator to the exact position we
 11006              are interested in.  Note that, even if we are already at
 11007              IT_CHARPOS (*it), the call below is not a no-op, as it
 11008              will detect that we are at the end of the string, pop the
 11009              iterator, and compute it->current_x and it->hpos
 11010              correctly.  */
 11011           move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
 11012                       -1, -1, -1, MOVE_TO_POS);
 11013         }
 11014     }
 11015   else
 11016     {
 11017       struct it it2;
 11018       void *it2data = NULL;
 11019       ptrdiff_t start_charpos, orig_charpos, i;
 11020       int nchars_per_row
 11021         = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 11022       bool hit_pos_limit = false;
 11023       ptrdiff_t pos_limit;
 11024 
 11025       /* Start at the beginning of the screen line containing IT's
 11026          position.  This may actually move vertically backwards,
 11027          in case of overlays, so adjust dvpos accordingly.  */
 11028       dvpos += it->vpos;
 11029       orig_charpos = IT_CHARPOS (*it);
 11030       move_it_vertically_backward (it, 0);
 11031       dvpos -= it->vpos;
 11032 
 11033       /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
 11034          screen lines, and reseat the iterator there.  */
 11035       start_charpos = IT_CHARPOS (*it);
 11036       if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 11037         pos_limit = BEGV;
 11038       else
 11039         pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
 11040 
 11041       for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
 11042         back_to_previous_visible_line_start (it);
 11043       if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
 11044         hit_pos_limit = true;
 11045       reseat (it, it->current.pos, true);
 11046 
 11047       /* Move further back if we end up in a string or an image.  */
 11048       while (!IT_POS_VALID_AFTER_MOVE_P (it))
 11049         {
 11050           /* First try to move to start of display line.  */
 11051           dvpos += it->vpos;
 11052           move_it_vertically_backward (it, 0);
 11053           dvpos -= it->vpos;
 11054           if (IT_POS_VALID_AFTER_MOVE_P (it))
 11055             break;
 11056           /* If start of line is still in string or image,
 11057              move further back.  */
 11058           back_to_previous_visible_line_start (it);
 11059           reseat (it, it->current.pos, true);
 11060           dvpos--;
 11061         }
 11062 
 11063       it->current_x = it->hpos = 0;
 11064 
 11065       /* Above call may have moved too far if continuation lines
 11066          are involved.  Scan forward and see if it did.  */
 11067       SAVE_IT (it2, *it, it2data);
 11068       it2.vpos = it2.current_y = 0;
 11069       move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
 11070       it->vpos -= it2.vpos;
 11071       it->current_y -= it2.current_y;
 11072       it->current_x = it->hpos = 0;
 11073 
 11074       /* If we moved too far back, move IT some lines forward.  */
 11075       if (it2.vpos > -dvpos)
 11076         {
 11077           int delta = it2.vpos + dvpos;
 11078 
 11079           RESTORE_IT (&it2, &it2, it2data);
 11080           SAVE_IT (it2, *it, it2data);
 11081           move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
 11082           /* Move back again if we got too far ahead,
 11083              or didn't move at all.  */
 11084           if (it->vpos - it2.vpos > delta || IT_CHARPOS (*it) == orig_charpos)
 11085             RESTORE_IT (it, &it2, it2data);
 11086           else
 11087             bidi_unshelve_cache (it2data, true);
 11088         }
 11089       else if (hit_pos_limit && pos_limit > BEGV
 11090                && dvpos < 0 && it2.vpos < -dvpos)
 11091         {
 11092           /* If we hit the limit, but still didn't make it far enough
 11093              back, that means there's a display string with a newline
 11094              covering a large chunk of text, and that caused
 11095              back_to_previous_visible_line_start try to go too far.
 11096              Punish those who commit such atrocities by going back
 11097              until we've reached DVPOS, after lifting the limit, which
 11098              could make it slow for very long lines.  "If it hurts,
 11099              don't do that!"  */
 11100           dvpos += it2.vpos;
 11101           RESTORE_IT (it, it, it2data);
 11102           for (i = -dvpos; i > 0; --i)
 11103             {
 11104               back_to_previous_visible_line_start (it);
 11105               it->vpos--;
 11106             }
 11107           reseat_1 (it, it->current.pos, true);
 11108         }
 11109       else
 11110         RESTORE_IT (it, it, it2data);
 11111     }
 11112 }
 11113 
 11114 int
 11115 partial_line_height (struct it *it_origin)
 11116 {
 11117   /* In a buffer with very long and truncated lines, we ignore the
 11118      possibly-partial height of the last line in the window: it is too
 11119      expensive to compute that (since in most cases that involves
 11120      going all the way to ZV), and the effect of ignoring it is
 11121      relatively minor.  */
 11122   if (XBUFFER (it_origin->w->contents)->long_line_optimizations_p
 11123       && it_origin->line_wrap == TRUNCATE)
 11124     return 0;
 11125 
 11126   int partial_height;
 11127   void *it_data = NULL;
 11128   struct it it;
 11129   SAVE_IT (it, *it_origin, it_data);
 11130   move_it_to (&it, ZV, -1, it.last_visible_y, -1,
 11131               MOVE_TO_POS | MOVE_TO_Y);
 11132   if (it.what == IT_EOB)
 11133     {
 11134       int vis_height = it.last_visible_y - it.current_y;
 11135       int height = it.ascent + it.descent;
 11136       partial_height = (vis_height < height) ? vis_height : 0;
 11137     }
 11138   else
 11139     {
 11140       int last_line_y = it.current_y;
 11141       move_it_by_lines (&it, 1);
 11142       partial_height = (it.current_y > it.last_visible_y)
 11143         ? it.last_visible_y - last_line_y : 0;
 11144     }
 11145   RESTORE_IT (&it, &it, it_data);
 11146   return partial_height;
 11147 }
 11148 
 11149 /* Approximate move_it_in_display_line_to for very long and truncated
 11150    display lines, when moving horizontally.  This is used when the
 11151    buffer's long_line_optimizations_p flag is set.  It ignores various
 11152    complications, like different font sizes, invisible text, display
 11153    and overlay strings, and, to some degree, bidirectional text.  So
 11154    caveat emptor!
 11155 
 11156    Starting from IT's position, reseat IT after skipping NCHARS
 11157    characters or to the next newline/ZV, whichever comes first.  Return
 11158    what move_it_in_display_line_to would have returned in this case.  */
 11159 
 11160 static enum move_it_result
 11161 fast_move_it_horizontally (struct it *it, ptrdiff_t nchars)
 11162 {
 11163   ptrdiff_t nl_bytepos;
 11164   ptrdiff_t nl_pos = find_newline_no_quit (IT_CHARPOS (*it), IT_BYTEPOS (*it),
 11165                                            1, &nl_bytepos);
 11166   struct text_pos new_pos;
 11167   enum move_it_result move_result;
 11168 
 11169   if (nl_pos - IT_CHARPOS (*it) > nchars)
 11170     {
 11171       SET_TEXT_POS (new_pos,
 11172                     IT_CHARPOS (*it) + nchars,
 11173                     CHAR_TO_BYTE (IT_CHARPOS (*it) + nchars));
 11174       move_result = MOVE_X_REACHED;
 11175     }
 11176   else
 11177     {
 11178       if (nl_bytepos < ZV_BYTE
 11179           || (nl_bytepos > BEGV_BYTE
 11180               && FETCH_BYTE (nl_bytepos - 1) == '\n'))
 11181         {
 11182           nl_pos--;
 11183           nl_bytepos--;
 11184           move_result = MOVE_NEWLINE_OR_CR;
 11185         }
 11186       else
 11187         move_result = MOVE_POS_MATCH_OR_ZV;
 11188       SET_TEXT_POS (new_pos, nl_pos, nl_bytepos);
 11189     }
 11190   reseat (it, new_pos, false);
 11191   return move_result;
 11192 }
 11193 
 11194 /* Return true if IT points into the middle of a display vector.  */
 11195 
 11196 bool
 11197 in_display_vector_p (struct it *it)
 11198 {
 11199   return (it->method == GET_FROM_DISPLAY_VECTOR
 11200           && it->current.dpvec_index > 0
 11201           && it->dpvec + it->current.dpvec_index != it->dpend);
 11202 }
 11203 
 11204 /* This is like Fwindow_text_pixel_size but assumes that WINDOW's buffer
 11205    is the current buffer.  Fbuffer_text_pixel_size calls it after it has
 11206    set WINDOW's buffer to the buffer specified by its BUFFER_OR_NAME
 11207    argument.  */
 11208 static Lisp_Object
 11209 window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 11210                         Lisp_Object x_limit, Lisp_Object y_limit,
 11211                         Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11212 {
 11213   struct window *w = decode_live_window (window);
 11214   struct it it;
 11215   ptrdiff_t start, end, bpos;
 11216   struct text_pos startp;
 11217   void *itdata = NULL;
 11218   int c, max_x = 0, max_y = 0, x = 0, y = 0, vertical_offset = 0, doff = 0;
 11219 
 11220   if (NILP (from))
 11221     {
 11222       start = BEGV;
 11223       bpos = BEGV_BYTE;
 11224     }
 11225   else if (EQ (from, Qt))
 11226     {
 11227       start = BEGV;
 11228       bpos = BEGV_BYTE;
 11229       while (bpos < ZV_BYTE)
 11230         {
 11231           c = FETCH_BYTE (bpos);
 11232           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11233             break;
 11234           inc_both (&start, &bpos);
 11235         }
 11236       while (bpos > BEGV_BYTE)
 11237         {
 11238           dec_both (&start, &bpos);
 11239           c = FETCH_BYTE (bpos);
 11240           if (!(c == ' ' || c == '\t'))
 11241             break;
 11242         }
 11243     }
 11244   else if (CONSP (from))
 11245     {
 11246       start = clip_to_bounds (BEGV, fix_position (XCAR (from)), ZV);
 11247       bpos = CHAR_TO_BYTE (start);
 11248       CHECK_FIXNUM (XCDR (from));
 11249       vertical_offset = XFIXNUM (XCDR (from));
 11250     }
 11251   else
 11252     {
 11253       start = clip_to_bounds (BEGV, fix_position (from), ZV);
 11254       bpos = CHAR_TO_BYTE (start);
 11255     }
 11256 
 11257   SET_TEXT_POS (startp, start, bpos);
 11258 
 11259   if (NILP (to))
 11260     end = ZV;
 11261   else if (EQ (to, Qt))
 11262     {
 11263       end = ZV;
 11264       bpos = ZV_BYTE;
 11265       while (bpos > BEGV_BYTE)
 11266         {
 11267           dec_both (&end, &bpos);
 11268           c = FETCH_BYTE (bpos);
 11269           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11270             {
 11271               inc_both (&end, &bpos);
 11272               break;
 11273             }
 11274         }
 11275       while (bpos < ZV_BYTE)
 11276         {
 11277           c = fetch_char_advance (&end, &bpos);
 11278           if (!(c == ' ' || c == '\t'))
 11279             break;
 11280         }
 11281     }
 11282   else
 11283     end = clip_to_bounds (start, fix_position (to), ZV);
 11284 
 11285   if (RANGED_FIXNUMP (0, x_limit, INT_MAX))
 11286     max_x = XFIXNUM (x_limit);
 11287   else if (!NILP (x_limit))
 11288     max_x = INT_MAX;
 11289 
 11290   if (NILP (y_limit))
 11291     max_y = INT_MAX;
 11292   else if (RANGED_FIXNUMP (0, y_limit, INT_MAX))
 11293     max_y = XFIXNUM (y_limit);
 11294 
 11295   itdata = bidi_shelve_cache ();
 11296 
 11297   start_display (&it, w, startp);
 11298 
 11299   int start_y = it.current_y;
 11300 
 11301   /* It makes no sense to measure dimensions of region of text that
 11302      crosses the point where bidi reordering changes scan direction.
 11303      By using unidirectional movement here we at least support the use
 11304      case of measuring regions of text that have a uniformly R2L
 11305      directionality, and regions that begin and end in text of the
 11306      same directionality.  */
 11307   it.bidi_p = false;
 11308 
 11309   int start_x;
 11310   if (vertical_offset != 0)
 11311     {
 11312       int last_y;
 11313       it.current_y = 0;
 11314 
 11315       move_it_by_lines (&it, 0);
 11316 
 11317       /* `move_it_vertically_backward' is called by move_it_vertically
 11318          to move by a negative value (upwards), but it is not always
 11319          guaranteed to leave the iterator at or above the position
 11320          given by the offset, which this loop ensures.  */
 11321       if (vertical_offset < 0)
 11322         {
 11323           while (it.current_y > vertical_offset)
 11324             {
 11325               last_y = it.current_y;
 11326               move_it_vertically_backward (&it,
 11327                                            (abs (vertical_offset)
 11328                                             + it.current_y));
 11329 
 11330               if (it.current_y == last_y)
 11331                 break;
 11332             }
 11333         }
 11334       else
 11335         {
 11336           move_it_vertically (&it, vertical_offset);
 11337         }
 11338 
 11339       it.current_y = (WINDOW_TAB_LINE_HEIGHT (w)
 11340                       + WINDOW_HEADER_LINE_HEIGHT (w));
 11341       start = clip_to_bounds (BEGV, IT_CHARPOS (it), ZV);
 11342       start_y = it.current_y;
 11343       start_x = it.current_x;
 11344     }
 11345   else
 11346     {
 11347       /* Start at the beginning of the line containing FROM.  Otherwise
 11348          IT.current_x will be incorrectly set to zero at some arbitrary
 11349          non-zero X coordinate.  */
 11350       reseat_at_previous_visible_line_start (&it);
 11351       it.current_x = it.hpos = 0;
 11352       if (IT_CHARPOS (it) != start)
 11353         {
 11354           void *it1data = NULL;
 11355           struct it it1;
 11356 
 11357           SAVE_IT (it1, it, it1data);
 11358           move_it_to (&it, start, -1, -1, -1, MOVE_TO_POS);
 11359           /* We could have a display property at START, in which case
 11360              asking move_it_to to stop at START will overshoot and
 11361              stop at position after START.  So we try again, stopping
 11362              before START, and account for the width of the last
 11363              buffer position manually.  */
 11364           if (IT_CHARPOS (it) > start && start > BEGV)
 11365             {
 11366               ptrdiff_t it1pos = IT_CHARPOS (it1);
 11367               int it1_x = it1.current_x;
 11368 
 11369               RESTORE_IT (&it, &it1, it1data);
 11370               /* If START - 1 is the beginning of screen line,
 11371                  move_it_to will not move, so we need to use a
 11372                  lower-level move_it_in_display_line subroutine, and
 11373                  tell it to move just 1 pixel, so it stops at the next
 11374                  display element.  */
 11375               if (start - 1 > it1pos)
 11376                 move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11377               else
 11378                 move_it_in_display_line (&it, start, it1_x + 1,
 11379                                          MOVE_TO_POS | MOVE_TO_X);
 11380               move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11381               start_x = it.current_x;
 11382               /* If we didn't change our buffer position, the pixel
 11383                  width of what's here was not yet accounted for; do it
 11384                  manually.  */
 11385               if (IT_CHARPOS (it) == start - 1)
 11386                 start_x += it.pixel_width;
 11387             }
 11388           else
 11389             {
 11390               start_x = it.current_x;
 11391               bidi_unshelve_cache (it1data, true);
 11392             }
 11393         }
 11394       else
 11395         start_x = it.current_x;
 11396     }
 11397 
 11398   /* Now move to TO.  */
 11399   int move_op = MOVE_TO_POS | MOVE_TO_Y;
 11400   int to_x = -1;
 11401   it.current_y = start_y;
 11402   /* If FROM is on a newline, pretend that we start at the beginning
 11403      of the next line, because the newline takes no place on display.  */
 11404   if (FETCH_BYTE (start) == '\n')
 11405     it.current_x = 0;
 11406   if (!NILP (x_limit))
 11407     {
 11408       it.last_visible_x = max_x;
 11409       /* Actually, we never want move_it_to stop at to_x.  But to make
 11410          sure that move_it_in_display_line_to always moves far enough,
 11411          we set to_x to INT_MAX and specify MOVE_TO_X.  */
 11412       move_op |= MOVE_TO_X;
 11413       to_x = INT_MAX;
 11414     }
 11415 
 11416   void *it2data = NULL;
 11417   struct it it2;
 11418   SAVE_IT (it2, it, it2data);
 11419 
 11420   x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11421 
 11422   /* We could have a display property at END, in which case asking
 11423      move_it_to to stop at END will overshoot and stop at position
 11424      after END.  So we try again, stopping before END, and account for
 11425      the width of the last buffer position manually.  */
 11426   if (IT_CHARPOS (it) > end)
 11427     {
 11428       end--;
 11429       RESTORE_IT (&it, &it2, it2data);
 11430       x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11431       /* Add the width of the thing at TO, but only if we didn't
 11432          overshoot it; if we did, it is already accounted for.  Also,
 11433          account for the height of the thing at TO.  */
 11434       if (IT_CHARPOS (it) == end)
 11435         {
 11436           x += it.pixel_width;
 11437 
 11438           /* DTRT if ignore_line_at_end is t.  */
 11439           if (!NILP (ignore_line_at_end))
 11440             doff = (max (it.max_ascent, it.ascent)
 11441                     + max (it.max_descent, it.descent));
 11442           else
 11443             {
 11444               it.max_ascent = max (it.max_ascent, it.ascent);
 11445               it.max_descent = max (it.max_descent, it.descent);
 11446             }
 11447         }
 11448     }
 11449   else
 11450     bidi_unshelve_cache (it2data, true);
 11451 
 11452   if (!NILP (x_limit))
 11453     {
 11454       /* Don't return more than X-LIMIT.  */
 11455       if (x > max_x)
 11456         x = max_x;
 11457     }
 11458 
 11459   /* If text spans more than one screen line, we don't need to adjust
 11460      the x-span for start_x, since the second and subsequent lines
 11461      will begin at zero X coordinate.  */
 11462   if (it.current_y > start_y)
 11463     start_x = 0;
 11464 
 11465   /* Subtract height of header-line and tab-line which was counted
 11466      automatically by start_display.  */
 11467   if (!NILP (ignore_line_at_end))
 11468     y = (it.current_y + doff
 11469          - WINDOW_TAB_LINE_HEIGHT (w)
 11470          - WINDOW_HEADER_LINE_HEIGHT (w));
 11471   else
 11472     y = (it.current_y + it.max_ascent + it.max_descent + doff
 11473          - WINDOW_TAB_LINE_HEIGHT (w) - WINDOW_HEADER_LINE_HEIGHT (w));
 11474 
 11475   /* Don't return more than Y-LIMIT.  */
 11476   if (y > max_y)
 11477     y = max_y;
 11478 
 11479   if ((EQ (mode_lines, Qtab_line) || EQ (mode_lines, Qt))
 11480       && window_wants_tab_line (w))
 11481     /* Add height of tab-line as requested.  */
 11482     {
 11483       Lisp_Object window_tab_line_format
 11484         = window_parameter (w, Qtab_line_format);
 11485 
 11486       y = y + display_mode_line (w, TAB_LINE_FACE_ID,
 11487                                  NILP (window_tab_line_format)
 11488                                  ? BVAR (current_buffer, tab_line_format)
 11489                                  : window_tab_line_format);
 11490     }
 11491 
 11492   if ((EQ (mode_lines, Qheader_line) || EQ (mode_lines, Qt))
 11493       && window_wants_header_line (w))
 11494     {
 11495       Lisp_Object window_header_line_format
 11496         = window_parameter (w, Qheader_line_format);
 11497 
 11498       y = y + display_mode_line (w, HEADER_LINE_FACE_ID,
 11499                                  NILP (window_header_line_format)
 11500                                  ? BVAR (current_buffer, header_line_format)
 11501                                  : window_header_line_format);
 11502     }
 11503 
 11504   if ((EQ (mode_lines, Qmode_line) || EQ (mode_lines, Qt))
 11505       && window_wants_mode_line (w))
 11506     {
 11507       Lisp_Object window_mode_line_format
 11508         = window_parameter (w, Qmode_line_format);
 11509 
 11510       y = y + display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
 11511                                  NILP (window_mode_line_format)
 11512                                  ? BVAR (current_buffer, mode_line_format)
 11513                                  : window_mode_line_format);
 11514     }
 11515 
 11516   bidi_unshelve_cache (itdata, false);
 11517 
 11518   return (!vertical_offset
 11519           ? Fcons (make_fixnum (x - start_x), make_fixnum (y))
 11520           : list3i (x - start_x, y, start));
 11521 }
 11522 
 11523 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 7, 0,
 11524        doc: /* Return the size of the text of WINDOW's buffer in pixels.
 11525 WINDOW must be a live window and defaults to the selected one.  The
 11526 return value is a cons of the maximum pixel-width of any text line and
 11527 the pixel-height of all the text lines in the accessible portion of
 11528 buffer text.
 11529 
 11530 If FROM is a cons cell, the return value includes, in addition to the
 11531 dimensions, also a third element that provides the buffer position
 11532 from which measuring of the text dimensions was actually started.
 11533 
 11534 This function exists to allow Lisp programs to adjust the dimensions
 11535 of WINDOW to the buffer text it needs to display.
 11536 
 11537 The optional argument FROM, if non-nil, specifies the first text
 11538 position to consider, and defaults to the minimum accessible position
 11539 of the buffer.  If FROM is a cons, its car specifies a buffer
 11540 position, and its cdr specifies the vertical offset in pixels from
 11541 that position to the first screen line to be measured.  If FROM is t,
 11542 it stands for the minimum accessible position that starts a non-empty
 11543 line.  TO, if non-nil, specifies the last text position and defaults
 11544 to the maximum accessible position of the buffer.  If TO is t, it
 11545 stands for the maximum accessible position that ends a non-empty line.
 11546 
 11547 The optional argument X-LIMIT, if non-nil, specifies the maximum X
 11548 coordinate beyond which the text should be ignored.  It is therefore
 11549 also the maximum width that the function can return.  X-LIMIT nil or
 11550 omitted means to use the pixel-width of WINDOW's body.  This default
 11551 means text of truncated lines wider than the window will be ignored;
 11552 specify a non-nil value for X-LIMIT if lines are truncated and you need
 11553 to account for the truncated text.
 11554 
 11555 Use nil for X-LIMIT if you want to know how high WINDOW should become in
 11556 order to fit all of its buffer's text with the width of WINDOW
 11557 unaltered.  Use the maximum width WINDOW may assume if you intend to
 11558 change WINDOW's width.  Use t for the maximum possible value.  Since
 11559 calculating the width of long lines can take some time, it's always a
 11560 good idea to make this argument as small as possible; in particular, if
 11561 the buffer contains long lines that shall be truncated anyway.
 11562 
 11563 The optional argument Y-LIMIT, if non-nil, specifies the maximum Y
 11564 coordinate beyond which the text is to be ignored; it is therefore
 11565 also the maximum height that the function can return (excluding the
 11566 height of the mode- or header-line, if any).  Y-LIMIT nil or omitted
 11567 means consider all of the accessible portion of buffer text up to the
 11568 position specified by TO.  Since calculating the text height of a
 11569 large buffer can take some time, it makes sense to specify this
 11570 argument if the size of the buffer is large or unknown.
 11571 
 11572 Optional argument MODE-LINES nil or omitted means do not include the
 11573 height of the mode-, tab- or header-line of WINDOW in the return value.
 11574 If it is the symbol `mode-line', `tab-line' or `header-line', include
 11575 only the height of that line, if present, in the return value.  If t,
 11576 include the height of any of these, if present, in the return value.
 11577 
 11578 IGNORE-LINE-AT-END, if non-nil, means to not add the height of the
 11579 screen line that includes TO to the returned height of the text.  */)
 11580   (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
 11581    Lisp_Object y_limit, Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11582 {
 11583   struct window *w = decode_live_window (window);
 11584   struct buffer *b = XBUFFER (w->contents);
 11585   struct buffer *old_b = NULL;
 11586   Lisp_Object value;
 11587 
 11588   if (b != current_buffer)
 11589     {
 11590       old_b = current_buffer;
 11591       set_buffer_internal_1 (b);
 11592     }
 11593 
 11594   value = window_text_pixel_size (window, from, to, x_limit, y_limit, mode_lines,
 11595                                   ignore_line_at_end);
 11596 
 11597   if (old_b)
 11598     set_buffer_internal_1 (old_b);
 11599 
 11600   return value;
 11601 }
 11602 
 11603 DEFUN ("buffer-text-pixel-size", Fbuffer_text_pixel_size, Sbuffer_text_pixel_size, 0, 4, 0,
 11604        doc: /* Return size of whole text of BUFFER-OR-NAME in WINDOW.
 11605 BUFFER-OR-NAME must specify a live buffer or the name of a live buffer
 11606 and defaults to the current buffer.  WINDOW must be a live window and
 11607 defaults to the selected one.  The return value is a cons of the maximum
 11608 pixel-width of any text line and the pixel-height of all the text lines
 11609 of the buffer specified by BUFFER-OR-NAME.
 11610 
 11611 The optional arguments X-LIMIT and Y-LIMIT have the same meaning as with
 11612 `window-text-pixel-size'.
 11613 
 11614 Do not use this function if the buffer specified by BUFFER-OR-NAME is
 11615 already displayed in WINDOW.  `window-text-pixel-size' is cheaper in
 11616 that case because it does not have to temporarily show that buffer in
 11617 WINDOW.  */)
 11618   (Lisp_Object buffer_or_name, Lisp_Object window, Lisp_Object x_limit,
 11619    Lisp_Object y_limit)
 11620 {
 11621   struct window *w = decode_live_window (window);
 11622   struct buffer *b = (NILP (buffer_or_name)
 11623                       ? current_buffer
 11624                       : XBUFFER (Fget_buffer (buffer_or_name)));
 11625   Lisp_Object buffer, value;
 11626   specpdl_ref count = SPECPDL_INDEX ();
 11627 
 11628   XSETBUFFER (buffer, b);
 11629 
 11630   /* The unwind form of with_echo_area_buffer is what we need here to
 11631      make WINDOW temporarily show our buffer.  */
 11632   /* FIXME: Can we move this into the `if (!EQ (buffer, w->contents))`?  */
 11633   record_unwind_protect (unwind_with_echo_area_buffer,
 11634                          with_echo_area_buffer_unwind_data (w));
 11635 
 11636   set_buffer_internal_1 (b);
 11637 
 11638   if (!EQ (buffer, w->contents))
 11639     {
 11640       wset_buffer (w, buffer);
 11641       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 11642       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 11643     }
 11644 
 11645   value = window_text_pixel_size (window, Qnil, Qnil, x_limit, y_limit, Qnil,
 11646                                   Qnil);
 11647 
 11648   unbind_to (count, Qnil);
 11649 
 11650   return value;
 11651 }
 11652 
 11653 
 11654 DEFUN ("display--line-is-continued-p", Fdisplay__line_is_continued_p,
 11655        Sdisplay__line_is_continued_p, 0, 0, 0,
 11656        doc: /* Return non-nil if the current screen line is continued on display.  */)
 11657   (void)
 11658 {
 11659   struct buffer *oldb = current_buffer;
 11660   struct window *w = XWINDOW (selected_window);
 11661   enum move_it_result rc = MOVE_POS_MATCH_OR_ZV;
 11662 
 11663   set_buffer_internal_1 (XBUFFER (w->contents));
 11664 
 11665   if (PT < ZV)
 11666     {
 11667       struct text_pos startpos;
 11668       struct it it;
 11669       void *itdata;
 11670       /* Use a marker, since vertical-motion enters redisplay, which can
 11671          trigger fontifications, which in turn could modify buffer text.  */
 11672       Lisp_Object opoint = Fpoint_marker ();
 11673 
 11674       /* Make sure to start from the beginning of the current screen
 11675          line, so that move_it_in_display_line_to counts pixels correctly.  */
 11676       Fvertical_motion (make_fixnum (0), selected_window, Qnil);
 11677       SET_TEXT_POS (startpos, PT, PT_BYTE);
 11678       itdata = bidi_shelve_cache ();
 11679       start_display (&it, w, startpos);
 11680       /* If lines are truncated, no line is continued.  */
 11681       if (it.line_wrap != TRUNCATE)
 11682         {
 11683           it.glyph_row = NULL;
 11684           rc = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 11685         }
 11686       SET_PT_BOTH (marker_position (opoint), marker_byte_position (opoint));
 11687       bidi_unshelve_cache (itdata, false);
 11688     }
 11689   set_buffer_internal_1 (oldb);
 11690 
 11691   return rc == MOVE_LINE_CONTINUED ? Qt : Qnil;
 11692 }
 11693 
 11694 
 11695 /***********************************************************************
 11696                                Messages
 11697  ***********************************************************************/
 11698 
 11699 /* Return the number of arguments the format string FORMAT needs.  */
 11700 
 11701 static ptrdiff_t
 11702 format_nargs (char const *format)
 11703 {
 11704   ptrdiff_t nargs = 0;
 11705   for (char const *p = format; (p = strchr (p, '%')); p++)
 11706     if (p[1] == '%')
 11707       p++;
 11708     else
 11709       nargs++;
 11710   return nargs;
 11711 }
 11712 
 11713 /* Add a message with format string FORMAT and formatted arguments
 11714    to *Messages*.  */
 11715 
 11716 void
 11717 add_to_log (const char *format, ...)
 11718 {
 11719   va_list ap;
 11720   va_start (ap, format);
 11721   vadd_to_log (format, ap);
 11722   va_end (ap);
 11723 }
 11724 
 11725 void
 11726 vadd_to_log (char const *format, va_list ap)
 11727 {
 11728   ptrdiff_t form_nargs = format_nargs (format);
 11729   ptrdiff_t nargs = 1 + form_nargs;
 11730   Lisp_Object args[10];
 11731   eassert (nargs <= ARRAYELTS (args));
 11732   AUTO_STRING (args0, format);
 11733   args[0] = args0;
 11734   for (ptrdiff_t i = 1; i <= nargs; i++)
 11735     args[i] = va_arg (ap, Lisp_Object);
 11736   Lisp_Object msg = Qnil;
 11737   msg = Fformat_message (nargs, args);
 11738 
 11739   ptrdiff_t len = SBYTES (msg) + 1;
 11740   USE_SAFE_ALLOCA;
 11741   char *buffer = SAFE_ALLOCA (len);
 11742   memcpy (buffer, SDATA (msg), len);
 11743 
 11744   message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
 11745   SAFE_FREE ();
 11746 }
 11747 
 11748 
 11749 /* Output a newline in the *Messages* buffer if "needs" one.  */
 11750 
 11751 void
 11752 message_log_maybe_newline (void)
 11753 {
 11754   if (message_log_need_newline)
 11755     message_dolog ("", 0, true, false);
 11756 }
 11757 
 11758 
 11759 /* Add a string M of length NBYTES to the message log, optionally
 11760    terminated with a newline when NLFLAG is true.  MULTIBYTE, if
 11761    true, means interpret the contents of M as multibyte.  This
 11762    function calls low-level routines in order to bypass text property
 11763    hooks, etc. which might not be safe to run.
 11764 
 11765    This may GC (insert may run before/after change hooks),
 11766    so the buffer M must NOT point to a Lisp string.  */
 11767 
 11768 void
 11769 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
 11770 {
 11771   const unsigned char *msg = (const unsigned char *) m;
 11772 
 11773   if (!NILP (Vmemory_full))
 11774     return;
 11775 
 11776   if (!NILP (Vmessage_log_max))
 11777     {
 11778       struct buffer *oldbuf;
 11779       Lisp_Object oldpoint, oldbegv, oldzv;
 11780       int old_windows_or_buffers_changed = windows_or_buffers_changed;
 11781       ptrdiff_t point_at_end = 0;
 11782       ptrdiff_t zv_at_end = 0;
 11783       Lisp_Object old_deactivate_mark;
 11784 
 11785       old_deactivate_mark = Vdeactivate_mark;
 11786       oldbuf = current_buffer;
 11787 
 11788       /* Sanity check, in case the variable has been set to something
 11789          invalid.  */
 11790       if (! STRINGP (Vmessages_buffer_name))
 11791         Vmessages_buffer_name = build_string ("*Messages*");
 11792       /* Ensure the Messages buffer exists, and switch to it.
 11793          If we created it, set the major-mode.  */
 11794       bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
 11795       Fset_buffer (Fget_buffer_create (Vmessages_buffer_name, Qnil));
 11796       if (newbuffer
 11797           && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
 11798         call0 (intern ("messages-buffer-mode"));
 11799 
 11800       bset_undo_list (current_buffer, Qt);
 11801       bset_cache_long_scans (current_buffer, Qnil);
 11802 
 11803       oldpoint = message_dolog_marker1;
 11804       set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
 11805       oldbegv = message_dolog_marker2;
 11806       set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
 11807       oldzv = message_dolog_marker3;
 11808       set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
 11809 
 11810       if (PT == Z)
 11811         point_at_end = 1;
 11812       if (ZV == Z)
 11813         zv_at_end = 1;
 11814 
 11815       BEGV = BEG;
 11816       BEGV_BYTE = BEG_BYTE;
 11817       ZV = Z;
 11818       ZV_BYTE = Z_BYTE;
 11819       TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11820 
 11821       /* Insert the string--maybe converting multibyte to single byte
 11822          or vice versa, so that all the text fits the buffer.  */
 11823       if (multibyte
 11824           && NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11825         {
 11826           /* Convert a multibyte string to single-byte
 11827              for the *Message* buffer.  */
 11828           for (ptrdiff_t i = 0; i < nbytes; )
 11829             {
 11830               int char_bytes, c = check_char_and_length (msg + i, &char_bytes);
 11831               char work = CHAR_TO_BYTE8 (c);
 11832               insert_1_both (&work, 1, 1, true, false, false);
 11833               i += char_bytes;
 11834             }
 11835         }
 11836       else if (! multibyte
 11837                && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11838         {
 11839           /* Convert a single-byte string to multibyte
 11840              for the *Message* buffer.  */
 11841           for (ptrdiff_t i = 0; i < nbytes; i++)
 11842             {
 11843               int c = make_char_multibyte (msg[i]);
 11844               unsigned char str[MAX_MULTIBYTE_LENGTH];
 11845               int char_bytes = CHAR_STRING (c, str);
 11846               insert_1_both ((char *) str, 1, char_bytes, true, false, false);
 11847             }
 11848         }
 11849       else if (nbytes)
 11850         insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
 11851                        true, false, false);
 11852 
 11853       if (nlflag)
 11854         {
 11855           ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
 11856           intmax_t dups;
 11857 
 11858           /* Since we call del_range_both passing false for PREPARE,
 11859              we aren't prepared to run modification hooks (we could
 11860              end up calling modification hooks from another buffer and
 11861              only with AFTER=t, Bug#21824).  */
 11862           specpdl_ref count = SPECPDL_INDEX ();
 11863           specbind (Qinhibit_modification_hooks, Qt);
 11864 
 11865           insert_1_both ("\n", 1, 1, true, false, false);
 11866 
 11867           scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
 11868           this_bol = PT;
 11869           this_bol_byte = PT_BYTE;
 11870 
 11871           /* See if this line duplicates the previous one.
 11872              If so, combine duplicates.  */
 11873           if (this_bol > BEG)
 11874             {
 11875               scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
 11876               prev_bol = PT;
 11877               prev_bol_byte = PT_BYTE;
 11878 
 11879               dups = message_log_check_duplicate (prev_bol_byte,
 11880                                                   this_bol_byte);
 11881               if (dups)
 11882                 {
 11883                   del_range_both (prev_bol, prev_bol_byte,
 11884                                   this_bol, this_bol_byte, false);
 11885                   if (dups > 1)
 11886                     {
 11887                       char dupstr[sizeof " [ times]"
 11888                                   + INT_STRLEN_BOUND (dups)];
 11889 
 11890                       /* If you change this format, don't forget to also
 11891                          change message_log_check_duplicate.  */
 11892                       int duplen = sprintf (dupstr, " [%"PRIdMAX" times]",
 11893                                             dups);
 11894                       TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
 11895                       insert_1_both (dupstr, duplen, duplen,
 11896                                      true, false, true);
 11897                     }
 11898                 }
 11899             }
 11900 
 11901           /* If we have more than the desired maximum number of lines
 11902              in the *Messages* buffer now, delete the oldest ones.
 11903              This is safe because we don't have undo in this buffer.  */
 11904 
 11905           if (FIXNATP (Vmessage_log_max))
 11906             {
 11907               scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
 11908                             -XFIXNAT (Vmessage_log_max) - 1, false);
 11909               del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
 11910             }
 11911 
 11912           unbind_to (count, Qnil);
 11913         }
 11914       BEGV = marker_position (oldbegv);
 11915       BEGV_BYTE = marker_byte_position (oldbegv);
 11916 
 11917       if (zv_at_end)
 11918         {
 11919           ZV = Z;
 11920           ZV_BYTE = Z_BYTE;
 11921         }
 11922       else
 11923         {
 11924           ZV = marker_position (oldzv);
 11925           ZV_BYTE = marker_byte_position (oldzv);
 11926         }
 11927 
 11928       if (point_at_end)
 11929         TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11930       else
 11931         /* We can't do Fgoto_char (oldpoint) because it will run some
 11932            Lisp code.  */
 11933         TEMP_SET_PT_BOTH (marker_position (oldpoint),
 11934                           marker_byte_position (oldpoint));
 11935 
 11936       unchain_marker (XMARKER (oldpoint));
 11937       unchain_marker (XMARKER (oldbegv));
 11938       unchain_marker (XMARKER (oldzv));
 11939 
 11940       /* We called insert_1_both above with its 5th argument (PREPARE)
 11941          false, which prevents insert_1_both from calling
 11942          prepare_to_modify_buffer, which in turns prevents us from
 11943          incrementing windows_or_buffers_changed even if *Messages* is
 11944          shown in some window.  So we must manually set
 11945          windows_or_buffers_changed here to make up for that.  */
 11946       windows_or_buffers_changed = old_windows_or_buffers_changed;
 11947       bset_redisplay (current_buffer);
 11948 
 11949       set_buffer_internal (oldbuf);
 11950 
 11951       message_log_need_newline = !nlflag;
 11952       Vdeactivate_mark = old_deactivate_mark;
 11953     }
 11954 }
 11955 
 11956 
 11957 /* We are at the end of the buffer after just having inserted a newline.
 11958    (Note: We depend on the fact we won't be crossing the gap.)
 11959    Check to see if the most recent message looks a lot like the previous one.
 11960    Return 0 if different, 1 if the new one should just replace it, or a
 11961    value N > 1 if we should also append " [N times]".  */
 11962 
 11963 static intmax_t
 11964 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
 11965 {
 11966   ptrdiff_t i;
 11967   ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
 11968   bool seen_dots = false;
 11969   unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
 11970   unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
 11971 
 11972   for (i = 0; i < len; i++)
 11973     {
 11974       if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
 11975         seen_dots = true;
 11976       if (p1[i] != p2[i])
 11977         return seen_dots;
 11978     }
 11979   p1 += len;
 11980   if (*p1 == '\n')
 11981     return 2;
 11982   if (*p1++ == ' ' && *p1++ == '[')
 11983     {
 11984       char *pend;
 11985       intmax_t n = strtoimax ((char *) p1, &pend, 10);
 11986       if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
 11987         return n + 1;
 11988     }
 11989   return 0;
 11990 }
 11991 
 11992 
 11993 /* Display an echo area message M with a specified length of NBYTES
 11994    bytes.  The string may include null characters.  If M is not a
 11995    string, clear out any existing message, and let the mini-buffer
 11996    text show through.
 11997 
 11998    This function cancels echoing.  */
 11999 
 12000 void
 12001 message3 (Lisp_Object m)
 12002 {
 12003   clear_message (true, true);
 12004   cancel_echoing ();
 12005 
 12006   /* First flush out any partial line written with print.  */
 12007   message_log_maybe_newline ();
 12008   if (STRINGP (m))
 12009     {
 12010       ptrdiff_t nbytes = SBYTES (m);
 12011       bool multibyte = STRING_MULTIBYTE (m);
 12012       char *buffer;
 12013       USE_SAFE_ALLOCA;
 12014       SAFE_ALLOCA_STRING (buffer, m);
 12015       message_dolog (buffer, nbytes, true, multibyte);
 12016       SAFE_FREE ();
 12017     }
 12018   if (! inhibit_message)
 12019     message3_nolog (m);
 12020 }
 12021 
 12022 /* Log the message M to stderr.  Log an empty line if M is not a string.  */
 12023 
 12024 static void
 12025 message_to_stderr (Lisp_Object m)
 12026 {
 12027   if (noninteractive_need_newline)
 12028     {
 12029       noninteractive_need_newline = false;
 12030       errputc ('\n');
 12031     }
 12032   if (STRINGP (m))
 12033     {
 12034       Lisp_Object coding_system = Vlocale_coding_system;
 12035       Lisp_Object s;
 12036 
 12037       if (!NILP (Vcoding_system_for_write))
 12038         coding_system = Vcoding_system_for_write;
 12039       if (!NILP (coding_system))
 12040         s = code_convert_string_norecord (m, coding_system, true);
 12041       else
 12042         s = m;
 12043 
 12044       errwrite (SDATA (s), SBYTES (s));
 12045     }
 12046   if (STRINGP (m) || !cursor_in_echo_area)
 12047     errputc ('\n');
 12048 }
 12049 
 12050 /* The non-logging version of message3.
 12051    This does not cancel echoing, because it is used for echoing.
 12052    Perhaps we need to make a separate function for echoing
 12053    and make this cancel echoing.  */
 12054 
 12055 void
 12056 message3_nolog (Lisp_Object m)
 12057 {
 12058   struct frame *sf = SELECTED_FRAME ();
 12059 
 12060   if (FRAME_INITIAL_P (sf))
 12061     message_to_stderr (m);
 12062   /* Error messages get reported properly by cmd_error, so this must be just an
 12063      informative message; if the frame hasn't really been initialized yet, just
 12064      toss it.  */
 12065   else if (INTERACTIVE && sf->glyphs_initialized_p)
 12066     {
 12067       /* Get the frame containing the mini-buffer
 12068          that the selected frame is using.  */
 12069       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 12070       Lisp_Object frame = XWINDOW (mini_window)->frame;
 12071       struct frame *f = XFRAME (frame);
 12072 
 12073       if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
 12074         Fmake_frame_visible (frame);
 12075 
 12076       if (STRINGP (m) && SCHARS (m) > 0)
 12077         {
 12078           set_message (m);
 12079           if (minibuffer_auto_raise)
 12080             Fraise_frame (frame);
 12081           /* Assume we are not echoing.
 12082              (If we are, echo_now will override this.)  */
 12083           echo_message_buffer = Qnil;
 12084         }
 12085       else
 12086         clear_message (true, true);
 12087 
 12088       do_pending_window_change (false);
 12089       echo_area_display (true);
 12090       do_pending_window_change (false);
 12091       if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 12092         (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
 12093     }
 12094 }
 12095 
 12096 
 12097 /* Display a null-terminated echo area message M.  If M is 0, clear
 12098    out any existing message, and let the mini-buffer text show through.
 12099 
 12100    The buffer M must continue to exist until after the echo area gets
 12101    cleared or some other message gets displayed there.  Do not pass
 12102    text that is stored in a Lisp string.  Do not pass text in a buffer
 12103    that was alloca'd.  */
 12104 
 12105 void
 12106 message1 (const char *m)
 12107 {
 12108   message3 (m ? build_unibyte_string (m) : Qnil);
 12109 }
 12110 
 12111 
 12112 /* The non-logging counterpart of message1.  */
 12113 
 12114 void
 12115 message1_nolog (const char *m)
 12116 {
 12117   message3_nolog (m ? build_unibyte_string (m) : Qnil);
 12118 }
 12119 
 12120 /* Display a message M which contains a single %s
 12121    which gets replaced with STRING.  */
 12122 
 12123 void
 12124 message_with_string (const char *m, Lisp_Object string, bool log)
 12125 {
 12126   CHECK_STRING (string);
 12127 
 12128   bool need_message;
 12129   if (noninteractive)
 12130     need_message = !!m;
 12131   else if (!INTERACTIVE)
 12132     need_message = false;
 12133   else
 12134     {
 12135       /* The frame whose minibuffer we're going to display the message on.
 12136          It may be larger than the selected frame, so we need
 12137          to use its buffer, not the selected frame's buffer.  */
 12138       Lisp_Object mini_window;
 12139       struct frame *f, *sf = SELECTED_FRAME ();
 12140 
 12141       /* Get the frame containing the minibuffer
 12142          that the selected frame is using.  */
 12143       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12144       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12145 
 12146       /* Error messages get reported properly by cmd_error, so this must be
 12147          just an informative message; if the frame hasn't really been
 12148          initialized yet, just toss it.  */
 12149       need_message = f->glyphs_initialized_p;
 12150     }
 12151 
 12152   if (need_message)
 12153     {
 12154       AUTO_STRING (fmt, m);
 12155       Lisp_Object msg = CALLN (Fformat_message, fmt, string);
 12156 
 12157       if (noninteractive)
 12158         message_to_stderr (msg);
 12159       else
 12160         {
 12161           if (log)
 12162             message3 (msg);
 12163           else
 12164             message3_nolog (msg);
 12165 
 12166           /* Print should start at the beginning of the message
 12167              buffer next time.  */
 12168           message_buf_print = false;
 12169         }
 12170     }
 12171 }
 12172 
 12173 
 12174 /* Dump an informative message to the minibuf.  If M is 0, clear out
 12175    any existing message, and let the mini-buffer text show through.
 12176 
 12177    The message must be safe ASCII (because when Emacs is
 12178    non-interactive the message is sent straight to stderr without
 12179    encoding first) and the format must not contain ` or ' (because
 12180    this function does not account for `text-quoting-style').  If your
 12181    message and format do not fit into this category, convert your
 12182    arguments to Lisp objects and use Fmessage instead.  */
 12183 
 12184 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
 12185 vmessage (const char *m, va_list ap)
 12186 {
 12187   if (noninteractive)
 12188     {
 12189       if (m)
 12190         {
 12191           if (noninteractive_need_newline)
 12192             putc ('\n', stderr);
 12193           noninteractive_need_newline = false;
 12194           vfprintf (stderr, m, ap);
 12195           if (!cursor_in_echo_area)
 12196             putc ('\n', stderr);
 12197           fflush (stderr);
 12198         }
 12199     }
 12200   else if (INTERACTIVE)
 12201     {
 12202       /* The frame whose mini-buffer we're going to display the message
 12203          on.  It may be larger than the selected frame, so we need to
 12204          use its buffer, not the selected frame's buffer.  */
 12205       Lisp_Object mini_window;
 12206       struct frame *f, *sf = SELECTED_FRAME ();
 12207 
 12208       /* Get the frame containing the mini-buffer
 12209          that the selected frame is using.  */
 12210       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12211       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12212 
 12213       /* Error messages get reported properly by cmd_error, so this must be
 12214          just an informative message; if the frame hasn't really been
 12215          initialized yet, just toss it.  */
 12216       if (f->glyphs_initialized_p)
 12217         {
 12218           if (m)
 12219             {
 12220               ptrdiff_t len;
 12221               ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
 12222               USE_SAFE_ALLOCA;
 12223               char *message_buf = SAFE_ALLOCA (maxsize + 1);
 12224 
 12225               len = doprnt (message_buf, maxsize, m, 0, ap);
 12226 
 12227               message3 (make_string (message_buf, len));
 12228               SAFE_FREE ();
 12229             }
 12230           else
 12231             message1 (0);
 12232 
 12233           /* Print should start at the beginning of the message
 12234              buffer next time.  */
 12235           message_buf_print = false;
 12236         }
 12237     }
 12238 }
 12239 
 12240 /* See vmessage for restrictions on the text of the message.  */
 12241 void
 12242 message (const char *m, ...)
 12243 {
 12244   va_list ap;
 12245   va_start (ap, m);
 12246   vmessage (m, ap);
 12247   va_end (ap);
 12248 }
 12249 
 12250 
 12251 /* Display the current message in the current mini-buffer.  This is
 12252    only called from error handlers in process.c, and is not time
 12253    critical.  */
 12254 
 12255 void
 12256 update_echo_area (void)
 12257 {
 12258   if (!NILP (echo_area_buffer[0]))
 12259     {
 12260       Lisp_Object string;
 12261       string = Fcurrent_message ();
 12262       message3 (string);
 12263     }
 12264 }
 12265 
 12266 
 12267 /* Make sure echo area buffers in `echo_buffers' are live.
 12268    If they aren't, make new ones.  */
 12269 
 12270 static void
 12271 ensure_echo_area_buffers (void)
 12272 {
 12273   for (int i = 0; i < 2; i++)
 12274     if (!BUFFERP (echo_buffer[i])
 12275         || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
 12276       {
 12277         Lisp_Object old_buffer = echo_buffer[i];
 12278         static char const name_fmt[] = " *Echo Area %d*";
 12279         char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
 12280         AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
 12281         echo_buffer[i] = Fget_buffer_create (lname, Qnil);
 12282         bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
 12283         /* to force word wrap in echo area -
 12284            it was decided to postpone this*/
 12285         /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
 12286 
 12287         for (int j = 0; j < 2; j++)
 12288           if (EQ (old_buffer, echo_area_buffer[j]))
 12289             echo_area_buffer[j] = echo_buffer[i];
 12290       }
 12291 }
 12292 
 12293 
 12294 /* Call FN with args A1..A2 with either the current or last displayed
 12295    echo_area_buffer as current buffer.
 12296 
 12297    WHICH zero means use the current message buffer
 12298    echo_area_buffer[0].  If that is nil, choose a suitable buffer
 12299    from echo_buffer[] and clear it.
 12300 
 12301    WHICH > 0 means use echo_area_buffer[1].  If that is nil, choose a
 12302    suitable buffer from echo_buffer[] and clear it.
 12303 
 12304    If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
 12305    that the current message becomes the last displayed one, choose a
 12306    suitable buffer for echo_area_buffer[0], and clear it.
 12307 
 12308    Value is what FN returns.  */
 12309 
 12310 static bool
 12311 with_echo_area_buffer (struct window *w, int which,
 12312                        bool (*fn) (void *, Lisp_Object),
 12313                        void *a1, Lisp_Object a2)
 12314 {
 12315   Lisp_Object buffer;
 12316   bool this_one, the_other, clear_buffer_p, rc;
 12317   specpdl_ref count = SPECPDL_INDEX ();
 12318 
 12319   /* If buffers aren't live, make new ones.  */
 12320   ensure_echo_area_buffers ();
 12321 
 12322   clear_buffer_p = false;
 12323 
 12324   if (which == 0)
 12325     this_one = false, the_other = true;
 12326   else if (which > 0)
 12327     this_one = true, the_other = false;
 12328   else
 12329     {
 12330       this_one = false, the_other = true;
 12331       clear_buffer_p = true;
 12332 
 12333       /* We need a fresh one in case the current echo buffer equals
 12334          the one containing the last displayed echo area message.  */
 12335       if (!NILP (echo_area_buffer[this_one])
 12336           && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
 12337         echo_area_buffer[this_one] = Qnil;
 12338     }
 12339 
 12340   /* Choose a suitable buffer from echo_buffer[] if we don't
 12341      have one.  */
 12342   if (NILP (echo_area_buffer[this_one]))
 12343     {
 12344       echo_area_buffer[this_one]
 12345         = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
 12346            ? echo_buffer[the_other]
 12347            : echo_buffer[this_one]);
 12348       clear_buffer_p = true;
 12349     }
 12350 
 12351   buffer = echo_area_buffer[this_one];
 12352 
 12353   /* Don't get confused by reusing the buffer used for echoing
 12354      for a different purpose.  */
 12355   if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
 12356     cancel_echoing ();
 12357 
 12358   record_unwind_protect (unwind_with_echo_area_buffer,
 12359                          with_echo_area_buffer_unwind_data (w));
 12360 
 12361   /* Make the echo area buffer current.  Note that for display
 12362      purposes, it is not necessary that the displayed window's buffer
 12363      == current_buffer, except for text property lookup.  So, let's
 12364      only set that buffer temporarily here without doing a full
 12365      Fset_window_buffer.  We must also change w->pointm, though,
 12366      because otherwise an assertions in unshow_buffer fails, and Emacs
 12367      aborts.  */
 12368   set_buffer_internal_1 (XBUFFER (buffer));
 12369   if (w)
 12370     {
 12371       wset_buffer (w, buffer);
 12372       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 12373       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 12374     }
 12375 
 12376   bset_undo_list (current_buffer, Qt);
 12377   bset_read_only (current_buffer, Qnil);
 12378   specbind (Qinhibit_read_only, Qt);
 12379   specbind (Qinhibit_modification_hooks, Qt);
 12380 
 12381   if (clear_buffer_p && Z > BEG)
 12382     del_range (BEG, Z);
 12383 
 12384   eassert (BEGV >= BEG);
 12385   eassert (ZV <= Z && ZV >= BEGV);
 12386 
 12387   rc = fn (a1, a2);
 12388 
 12389   eassert (BEGV >= BEG);
 12390   eassert (ZV <= Z && ZV >= BEGV);
 12391 
 12392   unbind_to (count, Qnil);
 12393   return rc;
 12394 }
 12395 
 12396 
 12397 /* Save state that should be preserved around the call to the function
 12398    FN called in with_echo_area_buffer.  */
 12399 
 12400 static Lisp_Object
 12401 with_echo_area_buffer_unwind_data (struct window *w)
 12402 {
 12403   int i = 0;
 12404   Lisp_Object vector, tmp;
 12405 
 12406   /* Reduce consing by keeping one vector in
 12407      Vwith_echo_area_save_vector.  */
 12408   vector = Vwith_echo_area_save_vector;
 12409   Vwith_echo_area_save_vector = Qnil;
 12410 
 12411   if (NILP (vector))
 12412     vector = make_nil_vector (11);
 12413 
 12414   XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
 12415   ASET (vector, i, Vdeactivate_mark); ++i;
 12416   ASET (vector, i, make_fixnum (windows_or_buffers_changed)); ++i;
 12417 
 12418   if (w)
 12419     {
 12420       XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
 12421       ASET (vector, i, w->contents); ++i;
 12422       ASET (vector, i, make_fixnum (marker_position (w->pointm))); ++i;
 12423       ASET (vector, i, make_fixnum (marker_byte_position (w->pointm))); ++i;
 12424       ASET (vector, i, make_fixnum (marker_position (w->old_pointm))); ++i;
 12425       ASET (vector, i, make_fixnum (marker_byte_position (w->old_pointm))); ++i;
 12426       ASET (vector, i, make_fixnum (marker_position (w->start))); ++i;
 12427       ASET (vector, i, make_fixnum (marker_byte_position (w->start))); ++i;
 12428     }
 12429   else
 12430     {
 12431       int end = i + 8;
 12432       for (; i < end; ++i)
 12433         ASET (vector, i, Qnil);
 12434     }
 12435 
 12436   eassert (i == ASIZE (vector));
 12437   return vector;
 12438 }
 12439 
 12440 
 12441 /* Restore global state from VECTOR which was created by
 12442    with_echo_area_buffer_unwind_data.  */
 12443 
 12444 static void
 12445 unwind_with_echo_area_buffer (Lisp_Object vector)
 12446 {
 12447   set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
 12448   Vdeactivate_mark = AREF (vector, 1);
 12449   windows_or_buffers_changed = XFIXNAT (AREF (vector, 2));
 12450 
 12451   if (WINDOWP (AREF (vector, 3)))
 12452     {
 12453       struct window *w;
 12454       Lisp_Object buffer;
 12455 
 12456       w = XWINDOW (AREF (vector, 3));
 12457       buffer = AREF (vector, 4);
 12458 
 12459       wset_buffer (w, buffer);
 12460       set_marker_restricted_both (w->pointm, buffer,
 12461                                   XFIXNAT (AREF (vector, 5)),
 12462                                   XFIXNAT (AREF (vector, 6)));
 12463       set_marker_restricted_both (w->old_pointm, buffer,
 12464                                   XFIXNAT (AREF (vector, 7)),
 12465                                   XFIXNAT (AREF (vector, 8)));
 12466       set_marker_restricted_both (w->start, buffer,
 12467                                   XFIXNAT (AREF (vector, 9)),
 12468                                   XFIXNAT (AREF (vector, 10)));
 12469     }
 12470 
 12471   Vwith_echo_area_save_vector = vector;
 12472 }
 12473 
 12474 
 12475 /* Set up the echo area for use by print functions.  MULTIBYTE_P
 12476    means we will print multibyte.  */
 12477 
 12478 void
 12479 setup_echo_area_for_printing (bool multibyte_p)
 12480 {
 12481   /* If we can't find an echo area any more, exit.  */
 12482   if (! FRAME_LIVE_P (XFRAME (selected_frame)))
 12483     Fkill_emacs (Qnil, Qnil);
 12484 
 12485   ensure_echo_area_buffers ();
 12486 
 12487   if (!message_buf_print)
 12488     {
 12489       /* A message has been output since the last time we printed.
 12490          Choose a fresh echo area buffer.  */
 12491       if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12492         echo_area_buffer[0] = echo_buffer[1];
 12493       else
 12494         echo_area_buffer[0] = echo_buffer[0];
 12495 
 12496       /* Switch to that buffer and clear it.  */
 12497       set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12498       bset_truncate_lines (current_buffer, Qnil);
 12499 
 12500       if (Z > BEG)
 12501         {
 12502           specpdl_ref count = SPECPDL_INDEX ();
 12503           specbind (Qinhibit_read_only, Qt);
 12504           /* Note that undo recording is always disabled.  */
 12505           del_range (BEG, Z);
 12506           unbind_to (count, Qnil);
 12507         }
 12508       TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 12509 
 12510       /* Set up the buffer for the multibyteness we need.  We always
 12511          set it to be multibyte, except when
 12512          unibyte-display-via-language-environment is non-nil and the
 12513          buffer from which we are called is unibyte, because in that
 12514          case unibyte characters should not be displayed as octal
 12515          escapes.  */
 12516       if (unibyte_display_via_language_environment
 12517           && !multibyte_p
 12518           && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12519         Fset_buffer_multibyte (Qnil);
 12520       else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12521         Fset_buffer_multibyte (Qt);
 12522 
 12523       /* Raise the frame containing the echo area.  */
 12524       if (minibuffer_auto_raise)
 12525         {
 12526           struct frame *sf = SELECTED_FRAME ();
 12527           Lisp_Object mini_window;
 12528           mini_window = FRAME_MINIBUF_WINDOW (sf);
 12529           Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
 12530         }
 12531 
 12532       message_log_maybe_newline ();
 12533       message_buf_print = true;
 12534     }
 12535   else
 12536     {
 12537       if (NILP (echo_area_buffer[0]))
 12538         {
 12539           if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12540             echo_area_buffer[0] = echo_buffer[1];
 12541           else
 12542             echo_area_buffer[0] = echo_buffer[0];
 12543         }
 12544 
 12545       if (current_buffer != XBUFFER (echo_area_buffer[0]))
 12546         {
 12547           /* Someone switched buffers between print requests.  */
 12548           set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12549           bset_truncate_lines (current_buffer, Qnil);
 12550         }
 12551     }
 12552 }
 12553 
 12554 
 12555 /* Display an echo area message in window W.  Value is true if W's
 12556    height is changed.  If display_last_displayed_message_p,
 12557    display the message that was last displayed, otherwise
 12558    display the current message.  */
 12559 
 12560 static bool
 12561 display_echo_area (struct window *w)
 12562 {
 12563   bool no_message_p, window_height_changed_p;
 12564 
 12565   /* Temporarily disable garbage collections while displaying the echo
 12566      area.  This is done because a GC can print a message itself.
 12567      That message would modify the echo area buffer's contents while a
 12568      redisplay of the buffer is going on, and seriously confuse
 12569      redisplay.  */
 12570   specpdl_ref count = inhibit_garbage_collection ();
 12571 
 12572   /* If there is no message, we must call display_echo_area_1
 12573      nevertheless because it resizes the window.  But we will have to
 12574      reset the echo_area_buffer in question to nil at the end because
 12575      with_echo_area_buffer will set it to an empty buffer.  */
 12576   bool i = display_last_displayed_message_p;
 12577   /* According to the C99, C11 and C++11 standards, the integral value
 12578      of a "bool" is always 0 or 1, so this array access is safe here,
 12579      if oddly typed. */
 12580   no_message_p = NILP (echo_area_buffer[i]);
 12581 
 12582   window_height_changed_p
 12583     = with_echo_area_buffer (w, display_last_displayed_message_p,
 12584                              display_echo_area_1, w, Qnil);
 12585 
 12586   if (no_message_p)
 12587     echo_area_buffer[i] = Qnil;
 12588 
 12589   unbind_to (count, Qnil);
 12590   return window_height_changed_p;
 12591 }
 12592 
 12593 
 12594 /* Helper for display_echo_area.  Display the current buffer which
 12595    contains the current echo area message in window W, a mini-window,
 12596    a pointer to which is passed in A1.  A2 is currently not used.
 12597    Change the height of W so that all of the message is displayed.
 12598    Value is true if height of W was changed.  */
 12599 
 12600 static bool
 12601 display_echo_area_1 (void *a1, Lisp_Object a2)
 12602 {
 12603   struct window *w = a1;
 12604   Lisp_Object window;
 12605   struct text_pos start;
 12606 
 12607   /* We are about to enter redisplay without going through
 12608      redisplay_internal, so we need to forget these faces by hand
 12609      here.  */
 12610   forget_escape_and_glyphless_faces ();
 12611 
 12612   /* Do this before displaying, so that we have a large enough glyph
 12613      matrix for the display.  If we can't get enough space for the
 12614      whole text, display the last N lines.  That works by setting w->start.  */
 12615   bool window_height_changed_p = resize_mini_window (w, false);
 12616 
 12617   /* Use the starting position chosen by resize_mini_window.  */
 12618   SET_TEXT_POS_FROM_MARKER (start, w->start);
 12619 
 12620   /* Display.  */
 12621   clear_glyph_matrix (w->desired_matrix);
 12622   XSETWINDOW (window, w);
 12623   void *itdata = bidi_shelve_cache ();
 12624   try_window (window, start, 0);
 12625   bidi_unshelve_cache (itdata, false);
 12626 
 12627   return window_height_changed_p;
 12628 }
 12629 
 12630 
 12631 /* Resize the echo area window to exactly the size needed for the
 12632    currently displayed message, if there is one.  If a mini-buffer
 12633    is active, don't shrink it.  */
 12634 
 12635 void
 12636 resize_echo_area_exactly (void)
 12637 {
 12638   if (BUFFERP (echo_area_buffer[0])
 12639       && WINDOWP (echo_area_window))
 12640     {
 12641       struct window *w = XWINDOW (echo_area_window);
 12642       Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
 12643       bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
 12644                                               w, resize_exactly);
 12645       if (resized_p)
 12646         {
 12647           windows_or_buffers_changed = 42;
 12648           update_mode_lines = 30;
 12649           redisplay_internal ();
 12650         }
 12651     }
 12652 }
 12653 
 12654 
 12655 /* Callback function for with_echo_area_buffer, when used from
 12656    resize_echo_area_exactly.  A1 contains a pointer to the window to
 12657    resize, EXACTLY non-nil means resize the mini-window exactly to the
 12658    size of the text displayed.  Value is what resize_mini_window
 12659    returns.  */
 12660 
 12661 static bool
 12662 resize_mini_window_1 (void *a1, Lisp_Object exactly)
 12663 {
 12664   return resize_mini_window (a1, !NILP (exactly));
 12665 }
 12666 
 12667 
 12668 /* Resize mini-window W to fit the size of its contents.  EXACT_P
 12669    means size the window exactly to the size needed.  Otherwise, it's
 12670    only enlarged until W's buffer is empty.
 12671 
 12672    Set W->start to the right place to begin display.  If the whole
 12673    contents fit, start at the beginning.  Otherwise, start so as
 12674    to make the end of the contents appear.  This is particularly
 12675    important for y-or-n-p, but seems desirable generally.
 12676 
 12677    Value is true if the window height has been changed.  */
 12678 
 12679 bool
 12680 resize_mini_window (struct window *w, bool exact_p)
 12681 {
 12682   struct frame *f = XFRAME (w->frame);
 12683   int old_height = WINDOW_BOX_TEXT_HEIGHT (w);
 12684 
 12685   eassert (MINI_WINDOW_P (w));
 12686 
 12687   /* Don't resize windows while redisplaying a window; it would
 12688      confuse redisplay functions when the size of the window they are
 12689      displaying changes from under them.  Such a resizing can happen,
 12690      for instance, when which-func prints a long message while
 12691      we are running fontification-functions.  We're running these
 12692      functions with safe_call which binds inhibit-redisplay to t.  */
 12693   if (!NILP (Vinhibit_redisplay))
 12694     return false;
 12695 
 12696   /* By default, start display at the beginning.  */
 12697   if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12698     set_marker_both (w->start, w->contents,
 12699                      BUF_BEGV (XBUFFER (w->contents)),
 12700                      BUF_BEGV_BYTE (XBUFFER (w->contents)));
 12701 
 12702   /* Nil means don't try to resize.  */
 12703   if ((NILP (Vresize_mini_windows)
 12704        && (NILP (resize_mini_frames) || !FRAME_MINIBUF_ONLY_P (f)))
 12705       || (FRAME_X_P (f) && FRAME_OUTPUT_DATA (f) == NULL))
 12706     return false;
 12707 
 12708   if (FRAME_MINIBUF_ONLY_P (f))
 12709     {
 12710       if (!NILP (resize_mini_frames))
 12711         safe_call1 (Qwindow__resize_mini_frame, WINDOW_FRAME (w));
 12712     }
 12713   else
 12714     {
 12715       struct it it;
 12716       int unit = FRAME_LINE_HEIGHT (f);
 12717       int height, max_height;
 12718       struct text_pos start;
 12719       struct buffer *old_current_buffer = NULL;
 12720       int windows_height = FRAME_INNER_HEIGHT (f);
 12721 
 12722       if (current_buffer != XBUFFER (w->contents))
 12723         {
 12724           old_current_buffer = current_buffer;
 12725           set_buffer_internal (XBUFFER (w->contents));
 12726         }
 12727 
 12728       init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
 12729 
 12730       /* Compute the max. number of lines specified by the user.  */
 12731       if (FLOATP (Vmax_mini_window_height))
 12732         max_height = XFLOAT_DATA (Vmax_mini_window_height) * windows_height;
 12733       else if (FIXNUMP (Vmax_mini_window_height))
 12734         max_height = XFIXNUM (Vmax_mini_window_height) * unit;
 12735       else
 12736         max_height = windows_height / 4;
 12737 
 12738       /* Correct that max. height if it's bogus.  */
 12739       max_height = clip_to_bounds (unit, max_height, windows_height);
 12740 
 12741       /* Find out the height of the text in the window.  */
 12742       last_height = 0;
 12743       move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
 12744       /* If move_it_to moved to the next visible line after EOB,
 12745          account for the height of the last full line.  */
 12746       if (it.max_ascent == 0 && it.max_descent == 0)
 12747         {
 12748           height = it.current_y;
 12749           /* Don't add the last line's height if lines are truncated
 12750              and the text doesn't end in a newline.
 12751              FIXME: if the text ends in a newline from a display
 12752              property or an overlay string, they lose: the mini-window
 12753              might not show the last empty line.  */
 12754           if (!(it.line_wrap == TRUNCATE
 12755                 && it.current_x <= it.first_visible_x
 12756                 && ZV_BYTE > 1
 12757                 && FETCH_BYTE (ZV_BYTE - 1) != '\n'))
 12758             height += last_height;
 12759         }
 12760       else
 12761         height = it.current_y + it.max_ascent + it.max_descent;
 12762       height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
 12763 
 12764       /* Compute a suitable window start.  */
 12765       if (height > max_height)
 12766         {
 12767           height = (max_height / unit) * unit;
 12768           if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12769             {
 12770               init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
 12771               move_it_vertically_backward (&it, height - unit);
 12772               /* The following move is usually a no-op when the stuff
 12773                  displayed in the mini-window comes entirely from buffer
 12774                  text, but it is needed when some of it comes from overlay
 12775                  strings, especially when there's an after-string at ZV.
 12776                  This happens with some completion packages, like
 12777                  icomplete, ido-vertical, etc.  With those packages, if we
 12778                  don't force w->start to be at the beginning of a screen
 12779                  line, important parts of the stuff in the mini-window,
 12780                  such as user prompt, will be hidden from view.  */
 12781               move_it_by_lines (&it, 0);
 12782               start = it.current.pos;
 12783               /* Prevent redisplay_window from recentering, and thus from
 12784                  overriding the window-start point we computed here.  */
 12785               w->start_at_line_beg = false;
 12786               SET_MARKER_FROM_TEXT_POS (w->start, start);
 12787             }
 12788         }
 12789       else
 12790         {
 12791           SET_TEXT_POS (start, BEGV, BEGV_BYTE);
 12792           SET_MARKER_FROM_TEXT_POS (w->start, start);
 12793         }
 12794 
 12795       if (EQ (Vresize_mini_windows, Qgrow_only))
 12796         {
 12797           /* Let it grow only, until we display an empty message, in which
 12798              case the window shrinks again.  */
 12799           if (height > old_height)
 12800             grow_mini_window (w, height - old_height);
 12801           else if (height < old_height && (exact_p || BEGV == ZV))
 12802             shrink_mini_window (w);
 12803         }
 12804       else if (height != old_height)
 12805         /* Always resize to exact size needed.  */
 12806         grow_mini_window (w, height - old_height);
 12807 
 12808       if (old_current_buffer)
 12809         set_buffer_internal (old_current_buffer);
 12810     }
 12811 
 12812   return WINDOW_BOX_TEXT_HEIGHT (w) != old_height;
 12813 }
 12814 
 12815 
 12816 /* Value is the current message, a string, or nil if there is no
 12817    current message.  */
 12818 
 12819 Lisp_Object
 12820 current_message (void)
 12821 {
 12822   Lisp_Object msg;
 12823 
 12824   if (!BUFFERP (echo_area_buffer[0]))
 12825     msg = Qnil;
 12826   else
 12827     {
 12828       with_echo_area_buffer (0, 0, current_message_1, &msg, Qnil);
 12829       if (NILP (msg))
 12830         echo_area_buffer[0] = Qnil;
 12831     }
 12832 
 12833   return msg;
 12834 }
 12835 
 12836 
 12837 static bool
 12838 current_message_1 (void *a1, Lisp_Object a2)
 12839 {
 12840   Lisp_Object *msg = a1;
 12841 
 12842   if (Z > BEG)
 12843     *msg = make_buffer_string (BEG, Z, true);
 12844   else
 12845     *msg = Qnil;
 12846   return false;
 12847 }
 12848 
 12849 
 12850 /* Push the current message on Vmessage_stack for later restoration
 12851    by restore_message.  Value is true if the current message isn't
 12852    empty.  This is a relatively infrequent operation, so it's not
 12853    worth optimizing.  */
 12854 
 12855 bool
 12856 push_message (void)
 12857 {
 12858   Lisp_Object msg = current_message ();
 12859   Vmessage_stack = Fcons (msg, Vmessage_stack);
 12860   return STRINGP (msg);
 12861 }
 12862 
 12863 
 12864 /* Restore message display from the top of Vmessage_stack.  */
 12865 
 12866 void
 12867 restore_message (void)
 12868 {
 12869   eassert (CONSP (Vmessage_stack));
 12870   message3_nolog (XCAR (Vmessage_stack));
 12871 }
 12872 
 12873 
 12874 /* Handler for unwind-protect calling pop_message.  */
 12875 
 12876 void
 12877 pop_message_unwind (void)
 12878 {
 12879   /* Pop the top-most entry off Vmessage_stack.  */
 12880   eassert (CONSP (Vmessage_stack));
 12881   Vmessage_stack = XCDR (Vmessage_stack);
 12882 }
 12883 
 12884 
 12885 /* Check that Vmessage_stack is nil.  Called from emacs.c when Emacs
 12886    exits.  If the stack is not empty, we have a missing
 12887    pop_message_unwind somewhere.  */
 12888 
 12889 void
 12890 check_message_stack (void)
 12891 {
 12892   if (!NILP (Vmessage_stack))
 12893     emacs_abort ();
 12894 }
 12895 
 12896 void
 12897 clear_message_stack (void)
 12898 {
 12899   Vmessage_stack = Qnil;
 12900 }
 12901 
 12902 /* Truncate to NCHARS what will be displayed in the echo area the next
 12903    time we display it---but don't redisplay it now.  */
 12904 
 12905 void
 12906 truncate_echo_area (ptrdiff_t nchars)
 12907 {
 12908   if (nchars == 0)
 12909     echo_area_buffer[0] = Qnil;
 12910   else if (!noninteractive
 12911            && INTERACTIVE
 12912            && !NILP (echo_area_buffer[0]))
 12913     {
 12914       struct frame *sf = SELECTED_FRAME ();
 12915       /* Error messages get reported properly by cmd_error, so this must be
 12916          just an informative message; if the frame hasn't really been
 12917          initialized yet, just toss it.  */
 12918       if (sf->glyphs_initialized_p)
 12919         with_echo_area_buffer (0, 0, truncate_message_1,
 12920                                (void *) (intptr_t) nchars, Qnil);
 12921     }
 12922 }
 12923 
 12924 
 12925 /* Helper function for truncate_echo_area.  Truncate the current
 12926    message to at most NCHARS characters.  */
 12927 
 12928 static bool
 12929 truncate_message_1 (void *a1, Lisp_Object a2)
 12930 {
 12931   intptr_t nchars = (intptr_t) a1;
 12932   if (BEG + nchars < Z)
 12933     del_range (BEG + nchars, Z);
 12934   if (Z == BEG)
 12935     echo_area_buffer[0] = Qnil;
 12936   return false;
 12937 }
 12938 
 12939 extern intptr_t garbage_collection_inhibited;
 12940 
 12941 /* Set the current message to STRING.  */
 12942 
 12943 static void
 12944 set_message (Lisp_Object string)
 12945 {
 12946   Lisp_Object message = Qnil;
 12947 
 12948   eassert (STRINGP (string));
 12949 
 12950   if (FUNCTIONP (Vset_message_function)
 12951       /* FIXME: (bug#63253) We should really make the regexp engine re-entrant,
 12952          but in the mean time, let's ignore `set-message-function` when
 12953          called from `probably_quit`.  */
 12954       && !garbage_collection_inhibited)
 12955     {
 12956       specpdl_ref count = SPECPDL_INDEX ();
 12957       specbind (Qinhibit_quit, Qt);
 12958       message = safe_call1 (Vset_message_function, string);
 12959       unbind_to (count, Qnil);
 12960 
 12961       if (STRINGP (message))
 12962         {
 12963           string = message;
 12964           message = Qnil;
 12965         }
 12966     }
 12967 
 12968   if (NILP (message))
 12969     {
 12970       message_enable_multibyte = STRING_MULTIBYTE (string);
 12971 
 12972       with_echo_area_buffer (0, -1, set_message_1, 0, string);
 12973       message_buf_print = false;
 12974       help_echo_showing_p = false;
 12975     }
 12976 
 12977   if (STRINGP (Vdebug_on_message)
 12978       && STRINGP (string)
 12979       && fast_string_match (Vdebug_on_message, string) >= 0)
 12980     call_debugger (list2 (Qerror, string));
 12981 }
 12982 
 12983 
 12984 /* Helper function for set_message.  First argument is ignored and second
 12985    argument has the same meaning as for set_message.
 12986    This function is called with the echo area buffer being current.  */
 12987 
 12988 static bool
 12989 set_message_1 (void *a1, Lisp_Object string)
 12990 {
 12991   eassert (STRINGP (string));
 12992 
 12993   /* Change multibyteness of the echo buffer appropriately.  We always
 12994      set it to be multibyte, except when
 12995      unibyte-display-via-language-environment is non-nil and the
 12996      string to display is unibyte, because in that case unibyte
 12997      characters should not be displayed as octal escapes.  */
 12998   if (!message_enable_multibyte
 12999       && unibyte_display_via_language_environment
 13000       && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13001     Fset_buffer_multibyte (Qnil);
 13002   else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13003     Fset_buffer_multibyte (Qt);
 13004 
 13005   bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
 13006   if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
 13007     bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
 13008 
 13009   /* Insert new message at BEG.  */
 13010   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 13011 
 13012   /* This function takes care of single/multibyte conversion.
 13013      We just have to ensure that the echo area buffer has the right
 13014      setting of enable_multibyte_characters.  */
 13015   insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
 13016 
 13017   return false;
 13018 }
 13019 
 13020 
 13021 /* Clear messages.  CURRENT_P means clear the current message.
 13022    LAST_DISPLAYED_P means clear the message last displayed.  */
 13023 
 13024 void
 13025 clear_message (bool current_p, bool last_displayed_p)
 13026 {
 13027   Lisp_Object preserve = Qnil;
 13028 
 13029   if (current_p)
 13030     {
 13031       if (FUNCTIONP (Vclear_message_function)
 13032           /* FIXME: (bug#63253) Same as for `set-message-function` above.  */
 13033           && !garbage_collection_inhibited)
 13034         {
 13035           specpdl_ref count = SPECPDL_INDEX ();
 13036           specbind (Qinhibit_quit, Qt);
 13037           preserve = safe_call (1, Vclear_message_function);
 13038           unbind_to (count, Qnil);
 13039         }
 13040 
 13041       if (!EQ (preserve, Qdont_clear_message))
 13042         {
 13043           echo_area_buffer[0] = Qnil;
 13044           message_cleared_p = true;
 13045         }
 13046     }
 13047 
 13048   if (last_displayed_p)
 13049     echo_area_buffer[1] = Qnil;
 13050 
 13051   message_buf_print = false;
 13052 }
 13053 
 13054 /* Clear garbaged frames.
 13055 
 13056    This function is used where the old redisplay called
 13057    redraw_garbaged_frames which in turn called redraw_frame which in
 13058    turn called clear_frame.  The call to clear_frame was a source of
 13059    flickering.  I believe a clear_frame is not necessary.  It should
 13060    suffice in the new redisplay to invalidate all current matrices,
 13061    and ensure a complete redisplay of all windows.  */
 13062 
 13063 static void
 13064 clear_garbaged_frames (void)
 13065 {
 13066   if (frame_garbaged)
 13067     {
 13068       Lisp_Object tail, frame;
 13069       struct frame *sf = SELECTED_FRAME ();
 13070 
 13071       FOR_EACH_FRAME (tail, frame)
 13072         {
 13073           struct frame *f = XFRAME (frame);
 13074 
 13075           if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
 13076             {
 13077               if (f->resized_p
 13078                   /* It makes no sense to redraw a non-selected TTY
 13079                      frame, since that will actually clear the
 13080                      selected frame, and might leave the selected
 13081                      frame with corrupted display, if it happens not
 13082                      to be marked garbaged.  */
 13083                   && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
 13084                 redraw_frame (f);
 13085               else
 13086                 clear_current_matrices (f);
 13087 
 13088 #ifdef HAVE_WINDOW_SYSTEM
 13089               if (FRAME_WINDOW_P (f)
 13090                   && FRAME_RIF (f)->clear_under_internal_border)
 13091                 FRAME_RIF (f)->clear_under_internal_border (f);
 13092 #endif
 13093               fset_redisplay (f);
 13094               f->garbaged = false;
 13095               f->resized_p = false;
 13096             }
 13097         }
 13098 
 13099       frame_garbaged = false;
 13100     }
 13101 }
 13102 
 13103 
 13104 /* Redisplay the echo area of the selected frame.  If UPDATE_FRAME_P, update
 13105    selected_frame.  */
 13106 
 13107 static void
 13108 echo_area_display (bool update_frame_p)
 13109 {
 13110   Lisp_Object mini_window;
 13111   struct window *w;
 13112   struct frame *f;
 13113   bool window_height_changed_p = false;
 13114   struct frame *sf = SELECTED_FRAME ();
 13115 
 13116   mini_window = FRAME_MINIBUF_WINDOW (sf);
 13117   if (NILP (mini_window))
 13118     return;
 13119 
 13120   w = XWINDOW (mini_window);
 13121   f = XFRAME (WINDOW_FRAME (w));
 13122 
 13123   /* Don't display if frame is invisible or not yet initialized.  */
 13124   if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
 13125     return;
 13126 
 13127 #ifdef HAVE_WINDOW_SYSTEM
 13128   /* When Emacs starts, selected_frame may be the initial terminal
 13129      frame.  If we let this through, a message would be displayed on
 13130      the terminal.  */
 13131   if (FRAME_INITIAL_P (XFRAME (selected_frame)))
 13132     return;
 13133 #endif /* HAVE_WINDOW_SYSTEM */
 13134 
 13135   /* Redraw garbaged frames.  */
 13136   clear_garbaged_frames ();
 13137 
 13138   if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
 13139     {
 13140       echo_area_window = mini_window;
 13141       window_height_changed_p = display_echo_area (w);
 13142       w->must_be_updated_p = true;
 13143 
 13144       /* Update the display, unless called from redisplay_internal.
 13145          Also don't update the screen during redisplay itself.  The
 13146          update will happen at the end of redisplay, and an update
 13147          here could cause confusion.  */
 13148       if (update_frame_p && !redisplaying_p)
 13149         {
 13150           int n = 0;
 13151 
 13152           /* If the display update has been interrupted by pending
 13153              input, update mode lines in the frame.  Due to the
 13154              pending input, it might have been that redisplay hasn't
 13155              been called, so that mode lines above the echo area are
 13156              garbaged.  This looks odd, so we prevent it here.  */
 13157           if (!display_completed)
 13158             {
 13159               n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
 13160 
 13161 #ifdef HAVE_WINDOW_SYSTEM
 13162               if (FRAME_WINDOW_P (f)
 13163                   && FRAME_RIF (f)->clear_under_internal_border)
 13164                 FRAME_RIF (f)->clear_under_internal_border (f);
 13165 #endif
 13166             }
 13167 
 13168           if (window_height_changed_p
 13169               /* Don't do this if Emacs is shutting down.  Redisplay
 13170                  needs to run hooks.  */
 13171               && !NILP (Vrun_hooks))
 13172             {
 13173               /* Must update other windows.  Likewise as in other
 13174                  cases, don't let this update be interrupted by
 13175                  pending input.  */
 13176               specpdl_ref count = SPECPDL_INDEX ();
 13177               specbind (Qredisplay_dont_pause, Qt);
 13178               fset_redisplay (f);
 13179               redisplay_internal ();
 13180               unbind_to (count, Qnil);
 13181             }
 13182           else if (FRAME_WINDOW_P (f) && n == 0)
 13183             {
 13184               /* Window configuration is the same as before.
 13185                  Can do with a display update of the echo area,
 13186                  unless we displayed some mode lines.  */
 13187               update_single_window (w);
 13188               flush_frame (f);
 13189             }
 13190           else
 13191             update_frame (f, true, true);
 13192 
 13193           /* If cursor is in the echo area, make sure that the next
 13194              redisplay displays the minibuffer, so that the cursor will
 13195              be replaced with what the minibuffer wants.  */
 13196           if (cursor_in_echo_area)
 13197             wset_redisplay (XWINDOW (mini_window));
 13198         }
 13199     }
 13200   else if (!EQ (mini_window, selected_window))
 13201     wset_redisplay (XWINDOW (mini_window));
 13202 
 13203   /* Last displayed message is now the current message.  */
 13204   echo_area_buffer[1] = echo_area_buffer[0];
 13205   /* Inform read_char that we're not echoing.  */
 13206   echo_message_buffer = Qnil;
 13207 
 13208   /* Prevent redisplay optimization in redisplay_internal by resetting
 13209      this_line_start_pos.  This is done because the mini-buffer now
 13210      displays the message instead of its buffer text.  */
 13211   if (EQ (mini_window, selected_window))
 13212     CHARPOS (this_line_start_pos) = 0;
 13213 
 13214   if (window_height_changed_p)
 13215     {
 13216       fset_redisplay (f);
 13217 
 13218       /* If window configuration was changed, frames may have been
 13219          marked garbaged.  Clear them or we will experience
 13220          surprises wrt scrolling.
 13221          FIXME: How/why/when?  */
 13222       clear_garbaged_frames ();
 13223     }
 13224 }
 13225 
 13226 /* True if W's buffer was changed but not saved.  */
 13227 
 13228 static bool
 13229 window_buffer_changed (struct window *w)
 13230 {
 13231   struct buffer *b = XBUFFER (w->contents);
 13232 
 13233   eassert (BUFFER_LIVE_P (b));
 13234 
 13235   return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
 13236 }
 13237 
 13238 /* True if W has %c or %C in its mode line and mode line should be updated.  */
 13239 
 13240 static bool
 13241 mode_line_update_needed (struct window *w)
 13242 {
 13243   return (w->column_number_displayed != -1
 13244           && !(PT == w->last_point && !window_outdated (w))
 13245           && (w->column_number_displayed != current_column ()));
 13246 }
 13247 
 13248 /* True if window start of W is frozen and may not be changed during
 13249    redisplay.  */
 13250 
 13251 static bool
 13252 window_frozen_p (struct window *w)
 13253 {
 13254   if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
 13255     {
 13256       Lisp_Object window;
 13257 
 13258       XSETWINDOW (window, w);
 13259       if (MINI_WINDOW_P (w))
 13260         return false;
 13261       else if (EQ (window, selected_window))
 13262         return false;
 13263       else if (MINI_WINDOW_P (XWINDOW (selected_window))
 13264                && EQ (window, Vminibuf_scroll_window))
 13265         /* This special window can't be frozen too.  */
 13266         return false;
 13267       else
 13268         return true;
 13269     }
 13270   return false;
 13271 }
 13272 
 13273 /***********************************************************************
 13274                      Mode Lines and Frame Titles
 13275  ***********************************************************************/
 13276 
 13277 /* A buffer for constructing non-propertized mode-line strings and
 13278    frame titles in it; allocated from the heap in init_xdisp and
 13279    resized as needed in store_mode_line_noprop_char.  */
 13280 
 13281 static char *mode_line_noprop_buf;
 13282 
 13283 /* The buffer's end, and a current output position in it.  */
 13284 
 13285 static char *mode_line_noprop_buf_end;
 13286 static char *mode_line_noprop_ptr;
 13287 
 13288 #define MODE_LINE_NOPROP_LEN(start) \
 13289   ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
 13290 
 13291 static enum {
 13292   MODE_LINE_DISPLAY = 0,
 13293   MODE_LINE_TITLE,
 13294   MODE_LINE_NOPROP,
 13295   MODE_LINE_STRING
 13296 } mode_line_target;
 13297 
 13298 /* Alist that caches the results of :propertize.
 13299    Each element is (PROPERTIZED-STRING . PROPERTY-LIST).  */
 13300 static Lisp_Object mode_line_proptrans_alist;
 13301 
 13302 /* List of strings making up the mode-line.  */
 13303 static Lisp_Object mode_line_string_list;
 13304 
 13305 /* Base face property when building propertized mode line string.  */
 13306 static Lisp_Object mode_line_string_face;
 13307 static Lisp_Object mode_line_string_face_prop;
 13308 
 13309 
 13310 /* Unwind data for mode line strings */
 13311 
 13312 static Lisp_Object Vmode_line_unwind_vector;
 13313 
 13314 static Lisp_Object
 13315 format_mode_line_unwind_data (struct frame *target_frame,
 13316                               struct buffer *obuf,
 13317                               Lisp_Object owin,
 13318                               bool save_proptrans)
 13319 {
 13320   Lisp_Object vector, tmp;
 13321 
 13322   /* Reduce consing by keeping one vector in
 13323      Vwith_echo_area_save_vector.  */
 13324   vector = Vmode_line_unwind_vector;
 13325   Vmode_line_unwind_vector = Qnil;
 13326 
 13327   if (NILP (vector))
 13328     vector = make_nil_vector (12);
 13329 
 13330   ASET (vector, 0, make_fixnum (mode_line_target));
 13331   ASET (vector, 1, make_fixnum (MODE_LINE_NOPROP_LEN (0)));
 13332   ASET (vector, 2, mode_line_string_list);
 13333   ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
 13334   ASET (vector, 4, mode_line_string_face);
 13335   ASET (vector, 5, mode_line_string_face_prop);
 13336 
 13337   if (obuf)
 13338     XSETBUFFER (tmp, obuf);
 13339   else
 13340     tmp = Qnil;
 13341   ASET (vector, 6, tmp);
 13342   ASET (vector, 7, owin);
 13343   if (target_frame)
 13344     {
 13345       Lisp_Object buffer = XWINDOW (target_frame->selected_window)->contents;
 13346       struct buffer *b = XBUFFER (buffer);
 13347       struct buffer *cb = current_buffer;
 13348 
 13349       /* Similarly to `with-selected-window', if the operation selects
 13350          a window on another frame, we must restore that frame's
 13351          selected window, and (for a tty) the top-frame.  */
 13352       ASET (vector, 8, target_frame->selected_window);
 13353       if (FRAME_TERMCAP_P (target_frame))
 13354         ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
 13355 
 13356       /* If we select a window on another frame, make sure that that
 13357          selection does not leave its buffer's point modified when
 13358          unwinding (Bug#32777).  */
 13359       ASET (vector, 10, buffer);
 13360       current_buffer = b;
 13361       ASET (vector, 11, build_marker (current_buffer, PT, PT_BYTE));
 13362       current_buffer = cb;
 13363     }
 13364 
 13365   return vector;
 13366 }
 13367 
 13368 static void
 13369 unwind_format_mode_line (Lisp_Object vector)
 13370 {
 13371   Lisp_Object old_window = AREF (vector, 7);
 13372   Lisp_Object target_frame_window = AREF (vector, 8);
 13373   Lisp_Object old_top_frame = AREF (vector, 9);
 13374 
 13375   mode_line_target = XFIXNUM (AREF (vector, 0));
 13376   mode_line_noprop_ptr = mode_line_noprop_buf + XFIXNUM (AREF (vector, 1));
 13377   mode_line_string_list = AREF (vector, 2);
 13378   if (! EQ (AREF (vector, 3), Qt))
 13379     mode_line_proptrans_alist = AREF (vector, 3);
 13380   mode_line_string_face = AREF (vector, 4);
 13381   mode_line_string_face_prop = AREF (vector, 5);
 13382 
 13383   /* Select window before buffer, since it may change the buffer.  */
 13384   if (WINDOW_LIVE_P (old_window))
 13385     {
 13386       /* If the operation that we are unwinding had selected a window
 13387          on a different frame, reset its frame-selected-window.  For a
 13388          text terminal, reset its top-frame if necessary.  */
 13389       if (WINDOW_LIVE_P (target_frame_window))
 13390         {
 13391           Lisp_Object frame
 13392             = WINDOW_FRAME (XWINDOW (target_frame_window));
 13393 
 13394           if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
 13395             Fselect_window (target_frame_window, Qt);
 13396 
 13397           if (!NILP (old_top_frame) && !EQ (old_top_frame, frame)
 13398               /* This could've been destroyed during the formatting,
 13399                  possibly because the terminal was deleted.  */
 13400               && FRAME_LIVE_P (XFRAME (old_top_frame)))
 13401             Fselect_frame (old_top_frame, Qt);
 13402         }
 13403 
 13404       Fselect_window (old_window, Qt);
 13405 
 13406       /* Restore point of target_frame_window's buffer (Bug#32777).
 13407          But do this only after old_window has been reselected to
 13408          avoid that the window point of target_frame_window moves.  */
 13409       if (WINDOW_LIVE_P (target_frame_window))
 13410         {
 13411           Lisp_Object buffer = AREF (vector, 10);
 13412 
 13413           if (BUFFER_LIVE_P (XBUFFER (buffer)))
 13414             {
 13415               struct buffer *cb = current_buffer;
 13416 
 13417               current_buffer = XBUFFER (buffer);
 13418               set_point_from_marker (AREF (vector, 11));
 13419               ASET (vector, 11, Qnil);
 13420               current_buffer = cb;
 13421             }
 13422         }
 13423     }
 13424 
 13425   if (!NILP (AREF (vector, 6)))
 13426     {
 13427       set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
 13428       ASET (vector, 6, Qnil);
 13429     }
 13430 
 13431   Vmode_line_unwind_vector = vector;
 13432 }
 13433 
 13434 
 13435 /* Store a single character C for the frame title in mode_line_noprop_buf.
 13436    Re-allocate mode_line_noprop_buf if necessary.  */
 13437 
 13438 static void
 13439 store_mode_line_noprop_char (char c)
 13440 {
 13441   /* If output position has reached the end of the allocated buffer,
 13442      increase the buffer's size.  */
 13443   if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
 13444     {
 13445       ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
 13446       ptrdiff_t size = len;
 13447       mode_line_noprop_buf =
 13448         xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
 13449       mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 13450       mode_line_noprop_ptr = mode_line_noprop_buf + len;
 13451     }
 13452 
 13453   *mode_line_noprop_ptr++ = c;
 13454 }
 13455 
 13456 
 13457 /* Store part of a frame title in mode_line_noprop_buf, beginning at
 13458    mode_line_noprop_ptr.  STRING is the string to store.  Do not copy
 13459    characters that yield more columns than PRECISION; PRECISION <= 0
 13460    means copy the whole string.  Pad with spaces until FIELD_WIDTH
 13461    number of characters have been copied; FIELD_WIDTH <= 0 means don't
 13462    pad.  Called from display_mode_element when it is used to build a
 13463    frame title.  */
 13464 
 13465 static int
 13466 store_mode_line_noprop (const char *string, int field_width, int precision)
 13467 {
 13468   const unsigned char *str = (const unsigned char *) string;
 13469   int n = 0;
 13470   ptrdiff_t dummy, nbytes;
 13471 
 13472   /* Copy at most PRECISION chars from STR.  */
 13473   nbytes = strlen (string);
 13474   n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
 13475   while (nbytes--)
 13476     store_mode_line_noprop_char (*str++);
 13477 
 13478   /* Fill up with spaces until FIELD_WIDTH reached.  */
 13479   while (field_width > 0
 13480          && n < field_width)
 13481     {
 13482       store_mode_line_noprop_char (' ');
 13483       ++n;
 13484     }
 13485 
 13486   return n;
 13487 }
 13488 
 13489 /***********************************************************************
 13490                              Frame Titles
 13491  ***********************************************************************/
 13492 
 13493 #ifdef HAVE_WINDOW_SYSTEM
 13494 
 13495 /* Set the title of FRAME, if it has changed.  The title format is
 13496    Vicon_title_format if FRAME is iconified, otherwise it is
 13497    frame_title_format.  */
 13498 
 13499 void
 13500 gui_consider_frame_title (Lisp_Object frame)
 13501 {
 13502   struct frame *f = XFRAME (frame);
 13503   Lisp_Object format_data;
 13504 
 13505   if ((FRAME_WINDOW_P (f)
 13506        || FRAME_MINIBUF_ONLY_P (f)
 13507        || f->explicit_name)
 13508       && !FRAME_TOOLTIP_P (f))
 13509     {
 13510       /* Do we have more than one visible frame on this X display?  */
 13511       Lisp_Object tail, other_frame, fmt;
 13512       ptrdiff_t title_start;
 13513       char *title;
 13514       ptrdiff_t len;
 13515       struct it it;
 13516       specpdl_ref count = SPECPDL_INDEX ();
 13517 
 13518       FOR_EACH_FRAME (tail, other_frame)
 13519         {
 13520           struct frame *tf = XFRAME (other_frame);
 13521 
 13522           if (tf != f
 13523               && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
 13524               && !FRAME_MINIBUF_ONLY_P (tf)
 13525               && !FRAME_PARENT_FRAME (tf)
 13526               && !FRAME_TOOLTIP_P (tf)
 13527               && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
 13528             break;
 13529         }
 13530 
 13531       /* Set global variable indicating that multiple frames exist.  */
 13532       multiple_frames = CONSP (tail);
 13533 
 13534       /* select-frame calls resize_mini_window, which could resize the
 13535          mini-window and by that undo the effect of this redisplay
 13536          cycle wrt minibuffer and echo-area display.  Binding
 13537          inhibit-redisplay to t makes the call to resize_mini_window a
 13538          no-op, thus avoiding the adverse side effects.  */
 13539 
 13540       /* The following was moved before the record_unwind_protect form
 13541          below to inhibit redisplay also when restoring the selected
 13542          window/frame: This avoids that resize_mini_window sizes back
 13543          the minibuffer window of a temporarily selected frame.  See
 13544          Bug#34317.  */
 13545       specbind (Qinhibit_redisplay, Qt);
 13546 
 13547       /* Switch to the buffer of selected window of the frame.  Set up
 13548          mode_line_target so that display_mode_element will output
 13549          into mode_line_noprop_buf; then display the title.  Save the
 13550          original frame and selected window, and possibly the topmost
 13551          frame of the tty (for tty frames) into a vector; it will be
 13552          restored later.  */
 13553 
 13554       format_data = format_mode_line_unwind_data (f, current_buffer,
 13555                                                   selected_window,
 13556                                                   false);
 13557       record_unwind_protect (unwind_format_mode_line, format_data);
 13558 
 13559       Fselect_window (f->selected_window, Qt);
 13560       set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->contents));
 13561       fmt = (FRAME_ICONIFIED_P (f) && !EQ (Vicon_title_format, Qt)
 13562              ? Vicon_title_format : Vframe_title_format);
 13563 
 13564       mode_line_target = MODE_LINE_TITLE;
 13565       title_start = MODE_LINE_NOPROP_LEN (0);
 13566       init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
 13567                      NULL, DEFAULT_FACE_ID);
 13568       display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
 13569       len = MODE_LINE_NOPROP_LEN (title_start);
 13570       title = mode_line_noprop_buf + title_start;
 13571       /* Make sure that any raw bytes in the title are properly
 13572          represented by their multibyte sequences.  */
 13573       ptrdiff_t nchars = 0;
 13574       len = str_as_multibyte ((unsigned char *) title,
 13575                               mode_line_noprop_buf_end - title,
 13576                               len, &nchars);
 13577       unbind_to (count, Qnil);
 13578 
 13579       /* Set the title only if it's changed.  This avoids consing in
 13580          the common case where it hasn't.  (If it turns out that we've
 13581          already wasted too much time by walking through the list with
 13582          display_mode_element, then we might need to optimize at a
 13583          higher level than this.)  */
 13584       if ((! STRINGP (f->name)
 13585            || SBYTES (f->name) != len
 13586            || memcmp (title, SDATA (f->name), len) != 0)
 13587           && FRAME_TERMINAL (f)->implicit_set_name_hook)
 13588         {
 13589           Lisp_Object title_string = make_multibyte_string (title, nchars, len);
 13590           FRAME_TERMINAL (f)->implicit_set_name_hook (f, title_string, Qnil);
 13591         }
 13592     }
 13593 }
 13594 
 13595 #endif /* not HAVE_WINDOW_SYSTEM */
 13596 
 13597 
 13598 /***********************************************************************
 13599                               Menu Bars
 13600  ***********************************************************************/
 13601 
 13602 /* True if we will not redisplay all visible windows.  */
 13603 #define REDISPLAY_SOME_P()                              \
 13604   ((windows_or_buffers_changed == 0                     \
 13605     || windows_or_buffers_changed == REDISPLAY_SOME)    \
 13606    && (update_mode_lines == 0                           \
 13607        || update_mode_lines == REDISPLAY_SOME))
 13608 
 13609 static bool
 13610 needs_no_redisplay (struct window *w)
 13611 {
 13612   struct buffer *buffer = XBUFFER (w->contents);
 13613   struct frame *f = XFRAME (w->frame);
 13614   return (REDISPLAY_SOME_P ()
 13615           && !w->redisplay
 13616           && !w->update_mode_line
 13617           && !f->face_change
 13618           && !f->redisplay
 13619           && !buffer->text->redisplay
 13620           && window_point (w) == w->last_point);
 13621 }
 13622 
 13623 /* Prepare for redisplay by updating menu-bar item lists when
 13624    appropriate.  This can call eval.  */
 13625 
 13626 static void
 13627 prepare_menu_bars (void)
 13628 {
 13629   bool all_windows = windows_or_buffers_changed || update_mode_lines;
 13630   bool some_windows = REDISPLAY_SOME_P ();
 13631 
 13632   if (FUNCTIONP (Vpre_redisplay_function))
 13633     {
 13634       Lisp_Object windows = all_windows ? Qt : Qnil;
 13635       if (all_windows && some_windows)
 13636         {
 13637           Lisp_Object ws = window_list ();
 13638           for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
 13639             {
 13640               Lisp_Object this = XCAR (ws);
 13641               struct window *w = XWINDOW (this);
 13642               /* Cf. conditions for redisplaying a window at the
 13643                  beginning of redisplay_window.  */
 13644               if (!needs_no_redisplay (w))
 13645                 windows = Fcons (this, windows);
 13646             }
 13647         }
 13648       safe__call1 (true, Vpre_redisplay_function, windows);
 13649     }
 13650 
 13651   /* Update all frame titles based on their buffer names, etc.  We do
 13652      this before the menu bars so that the buffer-menu will show the
 13653      up-to-date frame titles.  */
 13654 #ifdef HAVE_WINDOW_SYSTEM
 13655   if (all_windows)
 13656     {
 13657       Lisp_Object tail, frame;
 13658 
 13659       FOR_EACH_FRAME (tail, frame)
 13660         {
 13661           struct frame *f = XFRAME (frame);
 13662           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13663           if (some_windows
 13664               && !f->redisplay
 13665               && !w->redisplay
 13666               && !XBUFFER (w->contents)->text->redisplay)
 13667             continue;
 13668 
 13669           if (!FRAME_TOOLTIP_P (f)
 13670               && (FRAME_ICONIFIED_P (f)
 13671                   || FRAME_VISIBLE_P (f) == 1
 13672                   /* Exclude TTY frames that are obscured because they
 13673                      are not the top frame on their console.  This is
 13674                      because gui_consider_frame_title actually switches
 13675                      to the frame, which for TTY frames means it is
 13676                      marked as garbaged, and will be completely
 13677                      redrawn on the next redisplay cycle.  This causes
 13678                      TTY frames to be completely redrawn, when there
 13679                      are more than one of them, even though nothing
 13680                      should be changed on display.  */
 13681                   || (FRAME_VISIBLE_P (f) == 2 && FRAME_WINDOW_P (f))))
 13682             gui_consider_frame_title (frame);
 13683         }
 13684     }
 13685 #endif /* HAVE_WINDOW_SYSTEM */
 13686 
 13687   /* Update the menu bar item lists, if appropriate.  This has to be
 13688      done before any actual redisplay or generation of display lines.  */
 13689 
 13690   if (all_windows)
 13691     {
 13692       Lisp_Object tail, frame;
 13693       specpdl_ref count = SPECPDL_INDEX ();
 13694       /* True means that update_menu_bar has run its hooks
 13695          so any further calls to update_menu_bar shouldn't do so again.  */
 13696       bool menu_bar_hooks_run = false;
 13697 
 13698       record_unwind_save_match_data ();
 13699 
 13700       FOR_EACH_FRAME (tail, frame)
 13701         {
 13702           struct frame *f = XFRAME (frame);
 13703           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13704 
 13705           /* Ignore tooltip frame.  */
 13706           if (FRAME_TOOLTIP_P (f))
 13707             continue;
 13708 
 13709           if (some_windows
 13710               && !f->redisplay
 13711               && !w->redisplay
 13712               && !XBUFFER (w->contents)->text->redisplay)
 13713             continue;
 13714 
 13715           if (!FRAME_PARENT_FRAME (f))
 13716             menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
 13717 
 13718           update_tab_bar (f, false);
 13719 #ifdef HAVE_WINDOW_SYSTEM
 13720           update_tool_bar (f, false);
 13721 #endif
 13722         }
 13723 
 13724       unbind_to (count, Qnil);
 13725     }
 13726   else
 13727     {
 13728       struct frame *sf = SELECTED_FRAME ();
 13729 
 13730       if (!FRAME_PARENT_FRAME (sf))
 13731         update_menu_bar (sf, true, false);
 13732 
 13733       update_tab_bar (sf, true);
 13734 #ifdef HAVE_WINDOW_SYSTEM
 13735       update_tool_bar (sf, true);
 13736 #endif
 13737     }
 13738 }
 13739 
 13740 
 13741 /* Update the menu bar item list for frame F.  This has to be done
 13742    before we start to fill in any display lines, because it can call
 13743    eval.
 13744 
 13745    If SAVE_MATCH_DATA, we must save and restore it here.
 13746 
 13747    If HOOKS_RUN, a previous call to update_menu_bar
 13748    already ran the menu bar hooks for this redisplay, so there
 13749    is no need to run them again.  The return value is the
 13750    updated value of this flag, to pass to the next call.  */
 13751 
 13752 static bool
 13753 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
 13754 {
 13755   Lisp_Object window;
 13756   struct window *w;
 13757 
 13758   /* If called recursively during a menu update, do nothing.  This can
 13759      happen when, for instance, an activate-menubar-hook causes a
 13760      redisplay.  */
 13761   if (inhibit_menubar_update)
 13762     return hooks_run;
 13763 
 13764   window = FRAME_SELECTED_WINDOW (f);
 13765   w = XWINDOW (window);
 13766 
 13767   if (FRAME_WINDOW_P (f)
 13768       ?
 13769 #ifdef HAVE_EXT_MENU_BAR
 13770       FRAME_EXTERNAL_MENU_BAR (f)
 13771 #else
 13772       FRAME_MENU_BAR_LINES (f) > 0
 13773 #endif
 13774       : FRAME_MENU_BAR_LINES (f) > 0)
 13775     {
 13776       /* If the user has switched buffers or windows, we need to
 13777          recompute to reflect the new bindings.  But we'll
 13778          recompute when update_mode_lines is set too; that means
 13779          that people can use force-mode-line-update to request
 13780          that the menu bar be recomputed.  The adverse effect on
 13781          the rest of the redisplay algorithm is about the same as
 13782          windows_or_buffers_changed anyway.  */
 13783       if (windows_or_buffers_changed
 13784           /* This used to test w->update_mode_line, but we believe
 13785              there is no need to recompute the menu in that case.  */
 13786           || update_mode_lines
 13787           || window_buffer_changed (w))
 13788         {
 13789           struct buffer *prev = current_buffer;
 13790           specpdl_ref count = SPECPDL_INDEX ();
 13791 
 13792           specbind (Qinhibit_menubar_update, Qt);
 13793 
 13794           set_buffer_internal_1 (XBUFFER (w->contents));
 13795           if (save_match_data)
 13796             record_unwind_save_match_data ();
 13797           if (NILP (Voverriding_local_map_menu_flag))
 13798             {
 13799               specbind (Qoverriding_terminal_local_map, Qnil);
 13800               specbind (Qoverriding_local_map, Qnil);
 13801             }
 13802 
 13803           if (!hooks_run)
 13804             {
 13805               /* Run the Lucid hook.  */
 13806               safe_run_hooks (Qactivate_menubar_hook);
 13807 
 13808               /* If it has changed current-menubar from previous value,
 13809                  really recompute the menu-bar from the value.  */
 13810               safe_run_hooks (Qmenu_bar_update_hook);
 13811 
 13812               hooks_run = true;
 13813             }
 13814 
 13815           XSETFRAME (Vmenu_updating_frame, f);
 13816           fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
 13817 
 13818           /* Redisplay the menu bar in case we changed it.  */
 13819 #ifdef HAVE_EXT_MENU_BAR
 13820           if (FRAME_WINDOW_P (f))
 13821             {
 13822 #if defined (HAVE_NS)
 13823               /* All frames on Mac OS share the same menubar.  So only
 13824                  the selected frame should be allowed to set it.  */
 13825               if (f == SELECTED_FRAME ())
 13826 #endif
 13827                 set_frame_menubar (f, false);
 13828             }
 13829           else
 13830             /* On a terminal screen, the menu bar is an ordinary screen
 13831                line, and this makes it get updated.  */
 13832             w->update_mode_line = true;
 13833 #else /* ! (HAVE_EXT_MENU_BAR) */
 13834           /* In the non-toolkit version, the menu bar is an ordinary screen
 13835              line, and this makes it get updated.  */
 13836           w->update_mode_line = true;
 13837 #endif /* HAVE_EXT_MENU_BAR */
 13838 
 13839           unbind_to (count, Qnil);
 13840           set_buffer_internal_1 (prev);
 13841         }
 13842     }
 13843 
 13844   return hooks_run;
 13845 }
 13846 
 13847 
 13848 
 13849 /***********************************************************************
 13850                                Tab-bars
 13851  ***********************************************************************/
 13852 
 13853 /* Restore WINDOW as the selected window and its frame as the selected
 13854    frame.  If WINDOW is dead but the selected frame is live, make the
 13855    latter's selected window the selected window.  If both, WINDOW and
 13856    the selected frame, are dead, assign selected frame and window from
 13857    some arbitrary live frame.  Abort if no such frame can be found.  */
 13858 static void
 13859 restore_selected_window (Lisp_Object window)
 13860 {
 13861   if (WINDOW_LIVE_P (window))
 13862     /* If WINDOW is live, make it the selected window and its frame's
 13863        selected window and set the selected frame to its frame.  */
 13864     {
 13865       selected_window = window;
 13866       selected_frame = XWINDOW (window)->frame;
 13867       FRAME_SELECTED_WINDOW (XFRAME (selected_frame)) = window;
 13868     }
 13869   else if (FRAMEP (selected_frame) && FRAME_LIVE_P (XFRAME (selected_frame)))
 13870     /* If WINDOW is dead but the selected frame is still live, make the
 13871        latter's selected window the selected one.  */
 13872     selected_window = FRAME_SELECTED_WINDOW (XFRAME (selected_frame));
 13873   else
 13874     /* If WINDOW and the selected frame are dead, choose some live,
 13875        non-child and non-tooltip frame as the new selected frame and
 13876        make its selected window the selected window.  */
 13877     {
 13878       Lisp_Object tail;
 13879       Lisp_Object frame UNINIT;
 13880 
 13881       FOR_EACH_FRAME (tail, frame)
 13882         {
 13883           struct frame *f = XFRAME (frame);
 13884 
 13885           if (!FRAME_PARENT_FRAME (f) && !FRAME_TOOLTIP_P (f))
 13886             {
 13887               selected_frame = frame;
 13888               selected_window = FRAME_SELECTED_WINDOW (f);
 13889 
 13890               return;
 13891             }
 13892         }
 13893 
 13894       /* Abort if we cannot find a live frame.  */
 13895       emacs_abort ();
 13896     }
 13897 }
 13898 
 13899 /* Restore WINDOW, if live, as its frame's selected window.  */
 13900 static void
 13901 restore_frame_selected_window (Lisp_Object window)
 13902 {
 13903   if (WINDOW_LIVE_P (window))
 13904     /* If WINDOW is live, make it its frame's selected window.  If that
 13905        frame is the selected frame, make WINDOW the selected window as
 13906        well.  */
 13907     {
 13908       Lisp_Object frame = XWINDOW (window)->frame;
 13909 
 13910       FRAME_SELECTED_WINDOW (XFRAME (frame)) = window;
 13911       if (EQ (frame, selected_frame))
 13912         selected_window = window;
 13913     }
 13914 }
 13915 
 13916 /* Update the tab-bar item list for frame F.  This has to be done
 13917    before we start to fill in any display lines.  Called from
 13918    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 13919    and restore it here.  */
 13920 
 13921 static void
 13922 update_tab_bar (struct frame *f, bool save_match_data)
 13923 {
 13924   bool do_update = false;
 13925 
 13926 #ifdef HAVE_WINDOW_SYSTEM
 13927   if (FRAME_WINDOW_P (f) && WINDOWP (f->tab_bar_window)) {
 13928     if (WINDOW_TOTAL_LINES (XWINDOW (f->tab_bar_window)) > 0)
 13929       do_update = true;
 13930   }
 13931   else
 13932 #endif
 13933   if (FRAME_TAB_BAR_LINES (f) > 0)
 13934     do_update = true;
 13935 
 13936   if (do_update)
 13937     {
 13938       Lisp_Object window;
 13939       struct window *w;
 13940 
 13941       window = FRAME_SELECTED_WINDOW (f);
 13942       w = XWINDOW (window);
 13943 
 13944       /* If the user has switched buffers or windows, we need to
 13945          recompute to reflect the new bindings.  But we'll
 13946          recompute when update_mode_lines is set too; that means
 13947          that people can use force-mode-line-update to request
 13948          that the menu bar be recomputed.  The adverse effect on
 13949          the rest of the redisplay algorithm is about the same as
 13950          windows_or_buffers_changed anyway.  */
 13951       if (windows_or_buffers_changed
 13952           || w->update_mode_line
 13953           || update_mode_lines
 13954           || window_buffer_changed (w))
 13955         {
 13956           struct buffer *prev = current_buffer;
 13957           specpdl_ref count = SPECPDL_INDEX ();
 13958           Lisp_Object new_tab_bar;
 13959           int new_n_tab_bar;
 13960 
 13961           /* Set current_buffer to the buffer of the selected
 13962              window of the frame, so that we get the right local
 13963              keymaps.  */
 13964           set_buffer_internal_1 (XBUFFER (w->contents));
 13965 
 13966           /* Save match data, if we must.  */
 13967           if (save_match_data)
 13968             record_unwind_save_match_data ();
 13969 
 13970           /* Make sure that we don't accidentally use bogus keymaps.  */
 13971           if (NILP (Voverriding_local_map_menu_flag))
 13972             {
 13973               specbind (Qoverriding_terminal_local_map, Qnil);
 13974               specbind (Qoverriding_local_map, Qnil);
 13975             }
 13976 
 13977           /* We must temporarily set the selected frame to this frame
 13978              before calling tab_bar_items, because the calculation of
 13979              the tab-bar keymap uses the selected frame (see
 13980              `tab-bar-make-keymap' in tab-bar.el).  */
 13981           eassert (EQ (selected_window,
 13982                        /* Since we only explicitly preserve selected_frame,
 13983                           check that selected_window would be redundant.  */
 13984                        XFRAME (selected_frame)->selected_window));
 13985 #ifdef HAVE_WINDOW_SYSTEM
 13986           Lisp_Object frame;
 13987           record_unwind_protect (restore_selected_window, selected_window);
 13988           XSETFRAME (frame, f);
 13989           selected_frame = frame;
 13990           selected_window = FRAME_SELECTED_WINDOW (f);
 13991 #endif
 13992 
 13993           /* Build desired tab-bar items from keymaps.  */
 13994           new_tab_bar
 13995             = tab_bar_items (Fcopy_sequence (f->tab_bar_items),
 13996                               &new_n_tab_bar);
 13997 
 13998           /* Redisplay the tab-bar if we changed it.  */
 13999           if (new_n_tab_bar != f->n_tab_bar_items
 14000               || NILP (Fequal (new_tab_bar, f->tab_bar_items)))
 14001             {
 14002               /* Redisplay that happens asynchronously due to an expose event
 14003                  may access f->tab_bar_items.  Make sure we update both
 14004                  variables within BLOCK_INPUT so no such event interrupts.  */
 14005               block_input ();
 14006               fset_tab_bar_items (f, new_tab_bar);
 14007               f->n_tab_bar_items = new_n_tab_bar;
 14008               w->update_mode_line = true;
 14009               unblock_input ();
 14010             }
 14011 
 14012           unbind_to (count, Qnil);
 14013           set_buffer_internal_1 (prev);
 14014         }
 14015     }
 14016 }
 14017 
 14018 /* Redisplay the tab bar in the frame for window W.
 14019 
 14020    The tab bar of X frames that don't have X toolkit support is
 14021    displayed in a special window W->frame->tab_bar_window.
 14022 
 14023    The tab bar of terminal frames is treated specially as far as
 14024    glyph matrices are concerned.  Tab bar lines are not part of
 14025    windows, so the update is done directly on the frame matrix rows
 14026    for the tab bar.  */
 14027 
 14028 static void
 14029 display_tab_bar (struct window *w)
 14030 {
 14031   struct frame *f = XFRAME (WINDOW_FRAME (w));
 14032   struct it it;
 14033   Lisp_Object items;
 14034   int i;
 14035 
 14036   /* Don't do all this for graphical frames.  */
 14037 #ifdef HAVE_NTGUI
 14038   if (FRAME_W32_P (f))
 14039     return;
 14040 #endif
 14041 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14042   if (FRAME_X_P (f))
 14043     return;
 14044 #endif
 14045 
 14046 #ifdef HAVE_NS
 14047   if (FRAME_NS_P (f))
 14048     return;
 14049 #endif /* HAVE_NS */
 14050 
 14051 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14052   eassert (!FRAME_WINDOW_P (f));
 14053   init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14054                  + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14055                  TAB_BAR_FACE_ID);
 14056   it.first_visible_x = 0;
 14057   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14058 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 14059   if (FRAME_WINDOW_P (f))
 14060     {
 14061       /* Tab bar lines are displayed in the desired matrix of the
 14062          dummy window tab_bar_window.  */
 14063       struct window *tab_w;
 14064       tab_w = XWINDOW (f->tab_bar_window);
 14065       init_iterator (&it, tab_w, -1, -1, tab_w->desired_matrix->rows,
 14066                      TAB_BAR_FACE_ID);
 14067       it.first_visible_x = 0;
 14068       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14069     }
 14070   else
 14071 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 14072     {
 14073       /* This is a TTY frame, i.e. character hpos/vpos are used as
 14074          pixel x/y.  */
 14075       init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14076                      + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14077                      TAB_BAR_FACE_ID);
 14078       it.first_visible_x = 0;
 14079       it.last_visible_x = FRAME_COLS (f);
 14080     }
 14081 
 14082   /* FIXME: This should be controlled by a user option.  See the
 14083      comments in redisplay_tool_bar and display_mode_line about
 14084      this.  */
 14085   it.paragraph_embedding = L2R;
 14086 
 14087   /* Clear all rows of the tab bar.  */
 14088   for (i = 0; i < FRAME_TAB_BAR_LINES (f); ++i)
 14089     {
 14090       struct glyph_row *row = it.glyph_row + i;
 14091       clear_glyph_row (row);
 14092       row->enabled_p = true;
 14093       row->full_width_p = true;
 14094       row->reversed_p = false;
 14095     }
 14096 
 14097   /* Display all items of the tab bar.  */
 14098   items = it.f->tab_bar_items;
 14099   int j;
 14100   for (i = 0, j = 0; i < it.f->n_tab_bar_items; ++i, j += TAB_BAR_ITEM_NSLOTS)
 14101     {
 14102       Lisp_Object string = AREF (items, j + TAB_BAR_ITEM_CAPTION);
 14103 
 14104       /* Stop at nil string.  */
 14105       if (NILP (string))
 14106         break;
 14107 
 14108       if (it.current_x < it.last_visible_x)
 14109         display_string (NULL, string, Qnil, 0, 0, &it,
 14110                         SCHARS (string), 0, 0, STRING_MULTIBYTE (string));
 14111     }
 14112 
 14113   /* Fill out the line with spaces.  */
 14114   if (it.current_x < it.last_visible_x)
 14115     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 14116 
 14117   /* Compute the total height of the lines.  */
 14118   compute_line_metrics (&it);
 14119 }
 14120 
 14121 #ifdef HAVE_WINDOW_SYSTEM
 14122 
 14123 /* Set F->desired_tab_bar_string to a Lisp string representing frame
 14124    F's desired tab-bar contents.  F->tab_bar_items must have
 14125    been set up previously by calling prepare_menu_bars.  */
 14126 
 14127 static void
 14128 build_desired_tab_bar_string (struct frame *f)
 14129 {
 14130   int i;
 14131   Lisp_Object caption;
 14132 
 14133   caption = Qnil;
 14134 
 14135   /* Prepare F->desired_tab_bar_string.  Make a new string.  */
 14136   fset_desired_tab_bar_string (f, build_string (""));
 14137 
 14138   /* Put a `display' property on the string for the captions to display,
 14139      put a `menu_item' property on tab-bar items with a value that
 14140      is the index of the item in F's tab-bar item vector.  */
 14141   for (i = 0; i < f->n_tab_bar_items; ++i)
 14142     {
 14143 #define PROP(IDX) \
 14144   AREF (f->tab_bar_items, i * TAB_BAR_ITEM_NSLOTS + (IDX))
 14145 
 14146       caption = Fcopy_sequence (PROP (TAB_BAR_ITEM_CAPTION));
 14147 
 14148       /* Put a `display' text property on the string for the caption to
 14149          display.  Put a `menu-item' property on the string that gives
 14150          the start of this item's properties in the tab-bar items
 14151          vector.  */
 14152       AUTO_LIST2 (props, Qmenu_item, make_fixnum (i * TAB_BAR_ITEM_NSLOTS));
 14153 
 14154       Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14155                             props, caption);
 14156 
 14157       f->desired_tab_bar_string =
 14158         concat2 (f->desired_tab_bar_string, caption);
 14159 
 14160 #undef PROP
 14161     }
 14162 }
 14163 
 14164 
 14165 /* Display one line of the tab-bar of frame IT->f.
 14166 
 14167    HEIGHT specifies the desired height of the tab-bar line.
 14168    If the actual height of the glyph row is less than HEIGHT, the
 14169    row's height is increased to HEIGHT, and the icons are centered
 14170    vertically in the new height.
 14171 
 14172    If HEIGHT is -1, we are counting needed tab-bar lines, so don't
 14173    count a final empty row in case the tab-bar width exactly matches
 14174    the window width.
 14175 */
 14176 
 14177 static void
 14178 display_tab_bar_line (struct it *it, int height)
 14179 {
 14180   struct glyph_row *row = it->glyph_row;
 14181   int max_x = it->last_visible_x;
 14182   struct glyph *last;
 14183 
 14184   /* Don't extend on a previously drawn tab bar items (Bug#16058).  */
 14185   clear_glyph_row (row);
 14186   row->enabled_p = true;
 14187   row->y = it->current_y;
 14188 
 14189   /* Note that this isn't made use of if the face hasn't a box,
 14190      so there's no need to check the face here.  */
 14191   it->start_of_box_run_p = true;
 14192 
 14193   bool enough = false;
 14194   while (it->current_x < max_x)
 14195     {
 14196       int x, n_glyphs_before, i, nglyphs;
 14197       struct it it_before;
 14198 
 14199       /* Get the next display element.  */
 14200       if (!get_next_display_element (it))
 14201         {
 14202           /* Don't count empty row if we are counting needed tab-bar lines.  */
 14203           if (height < 0 && !it->hpos)
 14204             return;
 14205           break;
 14206         }
 14207 
 14208       /* Produce glyphs.  */
 14209       n_glyphs_before = row->used[TEXT_AREA];
 14210       it_before = *it;
 14211 
 14212       PRODUCE_GLYPHS (it);
 14213 
 14214       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 14215       i = 0;
 14216       x = it_before.current_x;
 14217       while (i < nglyphs)
 14218         {
 14219           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 14220 
 14221           if (x + glyph->pixel_width > max_x)
 14222             {
 14223               /* Glyph doesn't fit on line.  Backtrack.  */
 14224               row->used[TEXT_AREA] = n_glyphs_before;
 14225               *it = it_before;
 14226               /* If this is the only glyph on this line, it will never fit on the
 14227                  tab-bar, so skip it.  But ensure there is at least one glyph,
 14228                  so we don't accidentally disable the tab-bar.  */
 14229               if (n_glyphs_before == 0
 14230                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 14231                 break;
 14232               goto out;
 14233             }
 14234 
 14235           ++it->hpos;
 14236           x += glyph->pixel_width;
 14237           ++i;
 14238         }
 14239 
 14240       enough = ITERATOR_AT_END_OF_LINE_P (it);
 14241       set_iterator_to_next (it, true);
 14242 
 14243       /* Stop at line end.  */
 14244       if (enough)
 14245         break;
 14246     }
 14247 
 14248  out:;
 14249 
 14250   row->displays_text_p = row->used[TEXT_AREA] != 0;
 14251 
 14252   /* Use default face for the border below the tab bar.
 14253 
 14254      FIXME: When auto-resize-tab-bars is grow-only, there is
 14255      no additional border below the possibly empty tab-bar lines.
 14256      So to make the extra empty lines look "normal", we have to
 14257      use the tab-bar face for the border too.  */
 14258   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14259       && !EQ (Vauto_resize_tab_bars, Qgrow_only))
 14260     it->face_id = DEFAULT_FACE_ID;
 14261 
 14262   extend_face_to_end_of_line (it);
 14263   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 14264   last->right_box_line_p = true;
 14265   if (last == row->glyphs[TEXT_AREA])
 14266     last->left_box_line_p = true;
 14267 
 14268   /* Make line the desired height and center it vertically.  */
 14269   if ((height -= it->max_ascent + it->max_descent) > 0)
 14270     {
 14271       /* Don't add more than one line height.  */
 14272       height %= FRAME_LINE_HEIGHT (it->f);
 14273       it->max_ascent += height / 2;
 14274       it->max_descent += (height + 1) / 2;
 14275     }
 14276 
 14277   compute_line_metrics (it);
 14278 
 14279   /* If line is empty, make it occupy the rest of the tab-bar.  */
 14280   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 14281     {
 14282       row->height = row->phys_height = it->last_visible_y - row->y;
 14283       row->visible_height = row->height;
 14284       row->ascent = row->phys_ascent = 0;
 14285       row->extra_line_spacing = 0;
 14286     }
 14287 
 14288   row->full_width_p = true;
 14289   row->continued_p = false;
 14290   row->truncated_on_left_p = false;
 14291   row->truncated_on_right_p = false;
 14292 
 14293   it->current_x = it->hpos = 0;
 14294   it->current_y += row->height;
 14295   ++it->vpos;
 14296   ++it->glyph_row;
 14297 }
 14298 
 14299 
 14300 /* Value is the number of pixels needed to make all tab-bar items of
 14301    frame F visible.  The actual number of glyph rows needed is
 14302    returned in *N_ROWS if non-NULL.  */
 14303 static int
 14304 tab_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 14305 {
 14306   struct window *w = XWINDOW (f->tab_bar_window);
 14307   struct it it;
 14308   /* tab_bar_height is called from redisplay_tab_bar after building
 14309      the desired matrix, so use (unused) mode-line row as temporary row to
 14310      avoid destroying the first tab-bar row.  */
 14311   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 14312 
 14313   /* Initialize an iterator for iteration over
 14314      F->desired_tab_bar_string in the tab-bar window of frame F.  */
 14315   init_iterator (&it, w, -1, -1, temp_row, TAB_BAR_FACE_ID);
 14316   temp_row->reversed_p = false;
 14317   it.first_visible_x = 0;
 14318   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14319   reseat_to_string (&it, NULL, f->desired_tab_bar_string,
 14320                     0, 0, 0, STRING_MULTIBYTE (f->desired_tab_bar_string));
 14321   it.paragraph_embedding = L2R;
 14322 
 14323   clear_glyph_row (temp_row);
 14324   while (!ITERATOR_AT_END_P (&it))
 14325     {
 14326       it.glyph_row = temp_row;
 14327       display_tab_bar_line (&it, -1);
 14328     }
 14329   clear_glyph_row (temp_row);
 14330 
 14331   /* f->n_tab_bar_rows == 0 means "unknown"; -1 means no tab-bar.  */
 14332   if (n_rows)
 14333     *n_rows = it.vpos > 0 ? it.vpos : -1;
 14334 
 14335   if (pixelwise)
 14336     return it.current_y;
 14337   else
 14338     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 14339 }
 14340 
 14341 DEFUN ("tab-bar-height", Ftab_bar_height, Stab_bar_height,
 14342        0, 2, 0,
 14343        doc: /* Return the number of lines occupied by the tab bar of FRAME.
 14344 If FRAME is nil or omitted, use the selected frame.  Optional argument
 14345 PIXELWISE non-nil means return the height of the tab bar in pixels.  */)
 14346   (Lisp_Object frame, Lisp_Object pixelwise)
 14347 {
 14348   int height = 0;
 14349 
 14350   struct frame *f = decode_any_frame (frame);
 14351 
 14352   if (WINDOWP (f->tab_bar_window)
 14353       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tab_bar_window)) > 0)
 14354     {
 14355       update_tab_bar (f, true);
 14356       if (f->n_tab_bar_items)
 14357         {
 14358           build_desired_tab_bar_string (f);
 14359           height = tab_bar_height (f, NULL, !NILP (pixelwise));
 14360         }
 14361     }
 14362 
 14363   return make_fixnum (height);
 14364 }
 14365 
 14366 
 14367 /* Display the tab-bar of frame F.  Value is true if tab-bar's
 14368    height should be changed.  */
 14369 static bool
 14370 redisplay_tab_bar (struct frame *f)
 14371 {
 14372   struct window *w;
 14373   struct it it;
 14374   struct glyph_row *row;
 14375 
 14376   f->tab_bar_redisplayed = true;
 14377 
 14378   /* If frame hasn't a tab-bar window or if it is zero-height, don't
 14379      do anything.  This means you must start with tab-bar-lines
 14380      non-zero to get the auto-sizing effect.  Or in other words, you
 14381      can turn off tab-bars by specifying tab-bar-lines zero.  */
 14382   if (!WINDOWP (f->tab_bar_window)
 14383       || (w = XWINDOW (f->tab_bar_window),
 14384           WINDOW_TOTAL_LINES (w) == 0))
 14385     {
 14386       /* Even if we do not display a tab bar initially, still pretend
 14387          that we have resized it.  This avoids that a later activation
 14388          of the tab bar resizes the frame, despite of the fact that the
 14389          setting of 'frame-inhibit-implied-resize' should inhibit it
 14390          (Bug#52986).  */
 14391       f->tab_bar_resized = true;
 14392 
 14393       return false;
 14394     }
 14395 
 14396   /* Build a string that represents the contents of the tab-bar.  */
 14397   build_desired_tab_bar_string (f);
 14398 
 14399   int new_nrows;
 14400   int new_height = tab_bar_height (f, &new_nrows, true);
 14401 
 14402   if (f->n_tab_bar_rows == 0)
 14403     {
 14404       f->n_tab_bar_rows = new_nrows;
 14405       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 14406         frame_default_tab_bar_height = new_height;
 14407     }
 14408 
 14409   /* If new_height or new_nrows indicate that we need to enlarge or
 14410      shrink the tab-bar window, we can return right away.  */
 14411   if (new_nrows > f->n_tab_bar_rows
 14412       || (EQ (Vauto_resize_tab_bars, Qgrow_only)
 14413           && !f->minimize_tab_bar_window_p
 14414           && new_height > WINDOW_PIXEL_HEIGHT (w))
 14415       || (! EQ (Vauto_resize_tab_bars, Qgrow_only)
 14416           && new_height < WINDOW_PIXEL_HEIGHT (w)))
 14417     {
 14418       if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14419         FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14420       if (new_nrows != f->n_tab_bar_rows)
 14421         f->n_tab_bar_rows = new_nrows;
 14422       clear_glyph_matrix (w->desired_matrix);
 14423       f->fonts_changed = true;
 14424       return true;
 14425     }
 14426 
 14427   /* Set up an iterator for the tab-bar window.  */
 14428   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TAB_BAR_FACE_ID);
 14429   it.first_visible_x = 0;
 14430   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14431   row = it.glyph_row;
 14432   row->reversed_p = false;
 14433   reseat_to_string (&it, NULL, f->desired_tab_bar_string, 0, 0, 0,
 14434                     STRING_MULTIBYTE (f->desired_tab_bar_string));
 14435   /* FIXME: This should be controlled by a user option.  But it
 14436      doesn't make sense to have an R2L tab bar if the menu bar cannot
 14437      be drawn also R2L, and making the menu bar R2L is tricky due
 14438      tabkit-specific code that implements it.  If an R2L tab bar is
 14439      ever supported, display_tab_bar_line should also be augmented to
 14440      call unproduce_glyphs like display_line and display_string
 14441      do.  */
 14442   it.paragraph_embedding = L2R;
 14443 
 14444   /* Display as many lines as needed to display all tab-bar items.  */
 14445 
 14446   if (f->n_tab_bar_rows > 0)
 14447     {
 14448       int border, rows, height, extra;
 14449 
 14450       if (TYPE_RANGED_FIXNUMP (int, Vtab_bar_border))
 14451         border = XFIXNUM (Vtab_bar_border);
 14452       else if (EQ (Vtab_bar_border, Qinternal_border_width))
 14453         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 14454       else if (EQ (Vtab_bar_border, Qborder_width))
 14455         border = f->border_width;
 14456       else
 14457         border = 0;
 14458       if (border < 0)
 14459         border = 0;
 14460 
 14461       rows = f->n_tab_bar_rows;
 14462       height = max (1, (it.last_visible_y - border) / rows);
 14463       extra = it.last_visible_y - border - height * rows;
 14464 
 14465       while (it.current_y < it.last_visible_y)
 14466         {
 14467           int h = 0;
 14468           if (extra > 0 && rows-- > 0)
 14469             {
 14470               h = (extra + rows - 1) / rows;
 14471               extra -= h;
 14472             }
 14473           display_tab_bar_line (&it, height + h);
 14474         }
 14475     }
 14476   else
 14477     {
 14478       while (it.current_y < it.last_visible_y)
 14479         display_tab_bar_line (&it, 0);
 14480     }
 14481 
 14482   /* It doesn't make much sense to try scrolling in the tab-bar
 14483      window, so don't do it.  */
 14484   w->desired_matrix->no_scrolling_p = true;
 14485   w->must_be_updated_p = true;
 14486 
 14487   if (!NILP (Vauto_resize_tab_bars))
 14488     {
 14489       bool change_height_p = false;
 14490 
 14491       /* If we couldn't display everything, change the tab-bar's
 14492          height if there is room for more.  */
 14493       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 14494         change_height_p = true;
 14495 
 14496       /* We subtract 1 because display_tab_bar_line advances the
 14497          glyph_row pointer before returning to its caller.  We want to
 14498          examine the last glyph row produced by
 14499          display_tab_bar_line.  */
 14500       row = it.glyph_row - 1;
 14501 
 14502       /* If there are blank lines at the end, except for a partially
 14503          visible blank line at the end that is smaller than
 14504          FRAME_LINE_HEIGHT, change the tab-bar's height.  */
 14505       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14506           && row->height >= FRAME_LINE_HEIGHT (f))
 14507         change_height_p = true;
 14508 
 14509       /* If row displays tab-bar items, but is partially visible,
 14510          change the tab-bar's height.  */
 14511       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14512           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 14513         change_height_p = true;
 14514 
 14515       /* Resize windows as needed by changing the `tab-bar-lines'
 14516          frame parameter.  */
 14517       if (change_height_p)
 14518         {
 14519           int nrows;
 14520           int new_height = tab_bar_height (f, &nrows, true);
 14521 
 14522           change_height_p = ((EQ (Vauto_resize_tab_bars, Qgrow_only)
 14523                               && !f->minimize_tab_bar_window_p)
 14524                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 14525                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 14526           f->minimize_tab_bar_window_p = false;
 14527 
 14528           if (change_height_p)
 14529             {
 14530               if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14531                 FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14532               frame_default_tab_bar_height = new_height;
 14533               clear_glyph_matrix (w->desired_matrix);
 14534               f->n_tab_bar_rows = nrows;
 14535               f->fonts_changed = true;
 14536 
 14537               return true;
 14538             }
 14539         }
 14540     }
 14541 
 14542   f->minimize_tab_bar_window_p = false;
 14543   return false;
 14544 }
 14545 
 14546 /* Get information about the tab-bar item which is displayed in GLYPH
 14547    on frame F.  Return in *PROP_IDX the index where tab-bar item
 14548    properties start in F->tab_bar_items.  Return in CLOSE_P an
 14549    indication whether the click was on the close-tab icon of the tab.
 14550    Value is false if GLYPH doesn't display a tab-bar item.  */
 14551 
 14552 static bool
 14553 tab_bar_item_info (struct frame *f, struct glyph *glyph,
 14554                    int *prop_idx, bool *close_p)
 14555 {
 14556   Lisp_Object prop;
 14557   ptrdiff_t charpos;
 14558 
 14559   /* This function can be called asynchronously, which means we must
 14560      exclude any possibility that Fget_text_property signals an
 14561      error.  */
 14562   charpos = min (SCHARS (f->current_tab_bar_string), glyph->charpos);
 14563   charpos = max (0, charpos);
 14564 
 14565   /* Get the text property `menu-item' at pos. The value of that
 14566      property is the start index of this item's properties in
 14567      F->tab_bar_items.  */
 14568   prop = Fget_text_property (make_fixnum (charpos),
 14569                              Qmenu_item, f->current_tab_bar_string);
 14570   if (! FIXNUMP (prop))
 14571     return false;
 14572   *prop_idx = XFIXNUM (prop);
 14573 
 14574   *close_p = !NILP (Fget_text_property (make_fixnum (charpos),
 14575                                         Qclose_tab,
 14576                                         f->current_tab_bar_string));
 14577 
 14578   return true;
 14579 }
 14580 
 14581 
 14582 /* Get information about the tab-bar item at position X/Y on frame F.
 14583    Return in *GLYPH a pointer to the glyph of the tab-bar item in
 14584    the current matrix of the tab-bar window of F, or NULL if not
 14585    on a tab-bar item.  Return in *PROP_IDX the index of the tab-bar
 14586    item in F->tab_bar_items.  Value is
 14587 
 14588    -1   if X/Y is not on a tab-bar item
 14589    0    if X/Y is on the same item that was highlighted before.
 14590    1    otherwise.  */
 14591 
 14592 static int
 14593 get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 14594                    int *hpos, int *vpos, int *prop_idx, bool *close_p)
 14595 {
 14596   struct window *w = XWINDOW (f->tab_bar_window);
 14597   int area;
 14598 
 14599   /* Find the glyph under X/Y.  */
 14600   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 14601   if (*glyph == NULL)
 14602     return -1;
 14603 
 14604   /* Get the start of this tab-bar item's properties in
 14605      f->tab_bar_items.  */
 14606   if (!tab_bar_item_info (f, *glyph, prop_idx, close_p))
 14607     return -1;
 14608 
 14609   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14610 }
 14611 
 14612 
 14613 /* EXPORT:
 14614    Handle mouse button event on the tab-bar of frame F, at
 14615    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 14616    false for button release.  MODIFIERS is event modifiers for button
 14617    release.  */
 14618 
 14619 Lisp_Object
 14620 handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14621                       int modifiers)
 14622 {
 14623   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14624   struct window *w = XWINDOW (f->tab_bar_window);
 14625   int hpos, vpos, prop_idx;
 14626   bool close_p;
 14627   struct glyph *glyph;
 14628   Lisp_Object enabled_p;
 14629   int ts;
 14630 
 14631   frame_to_window_pixel_xy (w, &x, &y);
 14632   ts = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14633   if (ts == -1)
 14634     return Fcons (Qtab_bar, Qnil);
 14635 
 14636   /* If item is disabled, do nothing.  */
 14637   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14638   if (NILP (enabled_p))
 14639     return Qnil;
 14640 
 14641   if (down_p)
 14642     {
 14643       /* Show the clicked button in pressed state.  */
 14644       if (!NILP (Vmouse_highlight))
 14645         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 14646       f->last_tab_bar_item = prop_idx; /* record the pressed tab */
 14647     }
 14648   else
 14649     {
 14650       /* Show item in released state.  */
 14651       if (!NILP (Vmouse_highlight))
 14652         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 14653       f->last_tab_bar_item = -1;
 14654     }
 14655 
 14656   Lisp_Object caption =
 14657     Fcopy_sequence (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION));
 14658 
 14659   AUTO_LIST2 (props, Qmenu_item,
 14660               list3 (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_KEY),
 14661                      AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_BINDING),
 14662                      close_p ? Qt : Qnil));
 14663 
 14664   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14665                         props, caption);
 14666 
 14667   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14668 }
 14669 
 14670 
 14671 /* Possibly highlight a tab-bar item on frame F when mouse moves to
 14672    tab-bar window-relative coordinates X/Y.  Called from
 14673    note_mouse_highlight.  */
 14674 
 14675 static void
 14676 note_tab_bar_highlight (struct frame *f, int x, int y)
 14677 {
 14678   Lisp_Object window = f->tab_bar_window;
 14679   struct window *w = XWINDOW (window);
 14680   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14681   int hpos, vpos;
 14682   struct glyph *glyph;
 14683   struct glyph_row *row;
 14684   int i;
 14685   Lisp_Object enabled_p;
 14686   int prop_idx;
 14687   bool close_p;
 14688   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 14689   int rc;
 14690 
 14691   /* Function note_mouse_highlight is called with negative X/Y
 14692      values when mouse moves outside of the frame.  */
 14693   if (x <= 0 || y <= 0)
 14694     {
 14695       clear_mouse_face (hlinfo);
 14696       return;
 14697     }
 14698 
 14699   rc = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14700   if (rc < 0)
 14701     {
 14702       /* Not on tab-bar item.  */
 14703       clear_mouse_face (hlinfo);
 14704       return;
 14705     }
 14706   else if (rc == 0)
 14707     /* On same tab-bar item as before.  */
 14708     goto set_help_echo;
 14709 
 14710   clear_mouse_face (hlinfo);
 14711 
 14712   bool mouse_down_p = false;
 14713   /* Mouse is down, but on different tab-bar item?  Or alternatively,
 14714      the mouse might've been pressed somewhere we don't know about,
 14715      and then have moved onto the tab bar.  In this case,
 14716      last_tab_bar_item is -1, so we DTRT and behave like other
 14717      programs by displaying the item as sunken. */
 14718   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 14719   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 14720                   && f == dpyinfo->last_mouse_frame);
 14721 
 14722   if (mouse_down_p && f->last_tab_bar_item != prop_idx
 14723       && f->last_tab_bar_item != -1)
 14724     return;
 14725   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 14726 
 14727   /* If tab-bar item is not enabled, don't highlight it.  */
 14728   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14729   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 14730     {
 14731       /* Compute the x-position of the glyph.  In front and past the
 14732          image is a space.  We include this in the highlighted area.  */
 14733       row = MATRIX_ROW (w->current_matrix, vpos);
 14734       for (i = x = 0; i < hpos; ++i)
 14735         x += row->glyphs[TEXT_AREA][i].pixel_width;
 14736 
 14737       /* Record this as the current active region.  */
 14738       hlinfo->mouse_face_beg_col = hpos;
 14739       hlinfo->mouse_face_beg_row = vpos;
 14740       hlinfo->mouse_face_beg_x = x;
 14741       hlinfo->mouse_face_past_end = false;
 14742 
 14743       hlinfo->mouse_face_end_col = hpos + 1;
 14744       hlinfo->mouse_face_end_row = vpos;
 14745       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 14746       hlinfo->mouse_face_window = window;
 14747       hlinfo->mouse_face_face_id = TAB_BAR_FACE_ID;
 14748 
 14749       /* Display it as active.  */
 14750       show_mouse_face (hlinfo, draw);
 14751     }
 14752 
 14753  set_help_echo:
 14754 
 14755   /* Set help_echo_string to a help string to display for this tab-bar item.
 14756      XTread_socket does the rest.  */
 14757   help_echo_object = help_echo_window = Qnil;
 14758   help_echo_pos = -1;
 14759   help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_HELP);
 14760   if (NILP (help_echo_string))
 14761     help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION);
 14762 }
 14763 
 14764 #endif /* HAVE_WINDOW_SYSTEM */
 14765 
 14766 /* Find the tab-bar item at X coordinate and return its information.  */
 14767 static Lisp_Object
 14768 tty_get_tab_bar_item (struct frame *f, int x, int *prop_idx, bool *close_p)
 14769 {
 14770   ptrdiff_t clen = 0;
 14771 
 14772   for (int i = 0; i < f->n_tab_bar_items; i++)
 14773     {
 14774       Lisp_Object caption = AREF (f->tab_bar_items, (i * TAB_BAR_ITEM_NSLOTS
 14775                                                      + TAB_BAR_ITEM_CAPTION));
 14776       if (NILP (caption))
 14777         return Qnil;
 14778       clen += SCHARS (caption);
 14779       if (x < clen)
 14780         {
 14781           *prop_idx = i;
 14782           *close_p = !NILP (Fget_text_property (make_fixnum (SCHARS (caption)
 14783                                                              - (clen - x)),
 14784                                                 Qclose_tab,
 14785                                                 caption));
 14786           return caption;
 14787         }
 14788     }
 14789   return Qnil;
 14790 }
 14791 
 14792 /* Handle a mouse click at X/Y on the tab bar of TTY frame F.  If the
 14793    click was on the tab bar and was handled, populate the EVENT
 14794    structure, store it in keyboard queue, and return true; otherwise
 14795    return false.  MODIFIERS are event modifiers for generating the tab
 14796    release event.  */
 14797 Lisp_Object
 14798 tty_handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14799                           struct input_event *event)
 14800 {
 14801   /* Did they click on the tab bar?  */
 14802   if (y < FRAME_MENU_BAR_LINES (f)
 14803       || y >= FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f))
 14804     return Qnil;
 14805 
 14806   /* Find the tab-bar item where the X,Y coordinates belong.  */
 14807   int prop_idx;
 14808   bool close_p;
 14809   Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &close_p);
 14810 
 14811   if (NILP (caption))
 14812     return Qnil;
 14813 
 14814   if (NILP (AREF (f->tab_bar_items,
 14815                   prop_idx * TAB_BAR_ITEM_NSLOTS + TAB_BAR_ITEM_ENABLED_P)))
 14816     return Qnil;
 14817 
 14818   if (down_p)
 14819     f->last_tab_bar_item = prop_idx;
 14820   else
 14821     f->last_tab_bar_item = -1;
 14822 
 14823   caption = Fcopy_sequence (caption);
 14824 
 14825   AUTO_LIST2 (props, Qmenu_item,
 14826               list3 (AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14827                            + TAB_BAR_ITEM_KEY),
 14828                      AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14829                            + TAB_BAR_ITEM_BINDING),
 14830                      close_p ? Qt : Qnil));
 14831 
 14832   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14833                         props, caption);
 14834 
 14835   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14836 }
 14837 
 14838 
 14839 
 14840 /***********************************************************************
 14841                                Tool-bars
 14842  ***********************************************************************/
 14843 
 14844 #ifdef HAVE_WINDOW_SYSTEM
 14845 
 14846 /* Update the tool-bar item list for frame F.  This has to be done
 14847    before we start to fill in any display lines.  Called from
 14848    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 14849    and restore it here.  */
 14850 
 14851 static void
 14852 update_tool_bar (struct frame *f, bool save_match_data)
 14853 {
 14854 #ifdef HAVE_EXT_TOOL_BAR
 14855   bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
 14856 #else
 14857   bool do_update = (WINDOWP (f->tool_bar_window)
 14858                     && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
 14859 #endif
 14860 
 14861   if (do_update)
 14862     {
 14863       Lisp_Object window;
 14864       struct window *w;
 14865 
 14866       window = FRAME_SELECTED_WINDOW (f);
 14867       w = XWINDOW (window);
 14868 
 14869       /* If the user has switched buffers or windows, we need to
 14870          recompute to reflect the new bindings.  But we'll
 14871          recompute when update_mode_lines is set too; that means
 14872          that people can use force-mode-line-update to request
 14873          that the menu bar be recomputed.  The adverse effect on
 14874          the rest of the redisplay algorithm is about the same as
 14875          windows_or_buffers_changed anyway.  */
 14876       if (windows_or_buffers_changed
 14877           || w->update_mode_line
 14878           || update_mode_lines
 14879           || window_buffer_changed (w))
 14880         {
 14881           struct buffer *prev = current_buffer;
 14882           specpdl_ref count = SPECPDL_INDEX ();
 14883           Lisp_Object frame, new_tool_bar;
 14884           int new_n_tool_bar;
 14885 
 14886           /* Set current_buffer to the buffer of the selected
 14887              window of the frame, so that we get the right local
 14888              keymaps.  */
 14889           set_buffer_internal_1 (XBUFFER (w->contents));
 14890 
 14891           /* Save match data, if we must.  */
 14892           if (save_match_data)
 14893             record_unwind_save_match_data ();
 14894 
 14895           /* Make sure that we don't accidentally use bogus keymaps.  */
 14896           if (NILP (Voverriding_local_map_menu_flag))
 14897             {
 14898               specbind (Qoverriding_terminal_local_map, Qnil);
 14899               specbind (Qoverriding_local_map, Qnil);
 14900             }
 14901 
 14902           /* We must temporarily set the selected frame to this frame
 14903              before calling tool_bar_items, because the calculation of
 14904              the tool-bar keymap uses the selected frame (see
 14905              `tool-bar-make-keymap' in tool-bar.el).  */
 14906           eassert (EQ (selected_window,
 14907                        /* Since we only explicitly preserve selected_frame,
 14908                           check that selected_window would be redundant.  */
 14909                        XFRAME (selected_frame)->selected_window));
 14910           record_unwind_protect (restore_selected_window, selected_window);
 14911           XSETFRAME (frame, f);
 14912           selected_frame = frame;
 14913           selected_window = FRAME_SELECTED_WINDOW (f);
 14914 
 14915           /* Build desired tool-bar items from keymaps.  */
 14916           new_tool_bar
 14917             = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
 14918                               &new_n_tool_bar);
 14919 
 14920           /* Redisplay the tool-bar if we changed it.  */
 14921           if (new_n_tool_bar != f->n_tool_bar_items
 14922               || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
 14923             {
 14924               /* Redisplay that happens asynchronously due to an expose event
 14925                  may access f->tool_bar_items.  Make sure we update both
 14926                  variables within BLOCK_INPUT so no such event interrupts.  */
 14927               block_input ();
 14928               fset_tool_bar_items (f, new_tool_bar);
 14929               f->n_tool_bar_items = new_n_tool_bar;
 14930               w->update_mode_line = true;
 14931               unblock_input ();
 14932             }
 14933 
 14934           unbind_to (count, Qnil);
 14935           set_buffer_internal_1 (prev);
 14936         }
 14937     }
 14938 }
 14939 
 14940 #ifndef HAVE_EXT_TOOL_BAR
 14941 
 14942 /* Set F->desired_tool_bar_string to a Lisp string representing frame
 14943    F's desired tool-bar contents.  F->tool_bar_items must have
 14944    been set up previously by calling prepare_menu_bars.  */
 14945 
 14946 static void
 14947 build_desired_tool_bar_string (struct frame *f)
 14948 {
 14949   int i, size, size_needed;
 14950   Lisp_Object image, plist;
 14951 
 14952   image = plist = Qnil;
 14953 
 14954   /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
 14955      Otherwise, make a new string.  */
 14956 
 14957   /* The size of the string we might be able to reuse.  */
 14958   size = (STRINGP (f->desired_tool_bar_string)
 14959           ? SCHARS (f->desired_tool_bar_string)
 14960           : 0);
 14961 
 14962   /* We need one space in the string for each image.  */
 14963   size_needed = f->n_tool_bar_items;
 14964 
 14965   /* Reuse f->desired_tool_bar_string, if possible.  */
 14966   if (size < size_needed || NILP (f->desired_tool_bar_string))
 14967     fset_desired_tool_bar_string
 14968       (f, Fmake_string (make_fixnum (size_needed), make_fixnum (' '), Qnil));
 14969   else
 14970     {
 14971       AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
 14972       Fremove_text_properties (make_fixnum (0), make_fixnum (size),
 14973                                props, f->desired_tool_bar_string);
 14974     }
 14975 
 14976   /* Put a `display' property on the string for the images to display,
 14977      put a `menu_item' property on tool-bar items with a value that
 14978      is the index of the item in F's tool-bar item vector.  */
 14979   for (i = 0; i < f->n_tool_bar_items; ++i)
 14980     {
 14981 #define PROP(IDX) \
 14982   AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
 14983 
 14984       bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
 14985       bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
 14986       int hmargin, vmargin, relief, idx, end;
 14987 
 14988       /* If image is a vector, choose the image according to the
 14989          button state.  */
 14990       image = PROP (TOOL_BAR_ITEM_IMAGES);
 14991       if (VECTORP (image))
 14992         {
 14993           if (enabled_p)
 14994             idx = (selected_p
 14995                    ? TOOL_BAR_IMAGE_ENABLED_SELECTED
 14996                    : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
 14997           else
 14998             idx = (selected_p
 14999                    ? TOOL_BAR_IMAGE_DISABLED_SELECTED
 15000                    : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
 15001 
 15002           eassert (ASIZE (image) >= idx);
 15003           image = AREF (image, idx);
 15004         }
 15005       else
 15006         idx = -1;
 15007 
 15008       /* Ignore invalid image specifications.  */
 15009       if (!valid_image_p (image))
 15010         continue;
 15011 
 15012       /* Display the tool-bar button pressed, or depressed.  */
 15013       plist = Fcopy_sequence (XCDR (image));
 15014 
 15015       /* Compute margin and relief to draw.  */
 15016       relief = (tool_bar_button_relief >= 0
 15017                 ? min (tool_bar_button_relief,
 15018                        min (INT_MAX, MOST_POSITIVE_FIXNUM))
 15019                 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
 15020       hmargin = vmargin = relief;
 15021 
 15022       if (RANGED_FIXNUMP (1, Vtool_bar_button_margin,
 15023                            INT_MAX - max (hmargin, vmargin)))
 15024         {
 15025           hmargin += XFIXNAT (Vtool_bar_button_margin);
 15026           vmargin += XFIXNAT (Vtool_bar_button_margin);
 15027         }
 15028       else if (CONSP (Vtool_bar_button_margin))
 15029         {
 15030           if (RANGED_FIXNUMP (1, XCAR (Vtool_bar_button_margin),
 15031                                INT_MAX - hmargin))
 15032             hmargin += XFIXNAT (XCAR (Vtool_bar_button_margin));
 15033 
 15034           if (RANGED_FIXNUMP (1, XCDR (Vtool_bar_button_margin),
 15035                                INT_MAX - vmargin))
 15036             vmargin += XFIXNAT (XCDR (Vtool_bar_button_margin));
 15037         }
 15038 
 15039       if (auto_raise_tool_bar_buttons_p)
 15040         {
 15041           /* Add a `:relief' property to the image spec if the item is
 15042              selected.  */
 15043           if (selected_p)
 15044             {
 15045               plist = plist_put (plist, QCrelief, make_fixnum (-relief));
 15046               hmargin -= relief;
 15047               vmargin -= relief;
 15048             }
 15049         }
 15050       else
 15051         {
 15052           /* If image is selected, display it pressed, i.e. with a
 15053              negative relief.  If it's not selected, display it with a
 15054              raised relief.  */
 15055           plist = plist_put (plist, QCrelief,
 15056                              (selected_p
 15057                               ? make_fixnum (-relief)
 15058                               : make_fixnum (relief)));
 15059           hmargin -= relief;
 15060           vmargin -= relief;
 15061         }
 15062 
 15063       /* Put a margin around the image.  */
 15064       if (hmargin || vmargin)
 15065         {
 15066           if (hmargin == vmargin)
 15067             plist = plist_put (plist, QCmargin, make_fixnum (hmargin));
 15068           else
 15069             plist = plist_put (plist, QCmargin,
 15070                                Fcons (make_fixnum (hmargin),
 15071                                       make_fixnum (vmargin)));
 15072         }
 15073 
 15074       /* If button is not enabled, and we don't have special images
 15075          for the disabled state, make the image appear disabled by
 15076          applying an appropriate algorithm to it.  */
 15077       if (!enabled_p && idx < 0)
 15078         plist = plist_put (plist, QCconversion, Qdisabled);
 15079 
 15080       /* Put a `display' text property on the string for the image to
 15081          display.  Put a `menu-item' property on the string that gives
 15082          the start of this item's properties in the tool-bar items
 15083          vector.  */
 15084       image = Fcons (Qimage, plist);
 15085       AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
 15086                   make_fixnum (i * TOOL_BAR_ITEM_NSLOTS));
 15087 
 15088       /* Let the last image hide all remaining spaces in the tool bar
 15089          string.  The string can be longer than needed when we reuse a
 15090          previous string.  */
 15091       if (i + 1 == f->n_tool_bar_items)
 15092         end = SCHARS (f->desired_tool_bar_string);
 15093       else
 15094         end = i + 1;
 15095       Fadd_text_properties (make_fixnum (i), make_fixnum (end),
 15096                             props, f->desired_tool_bar_string);
 15097 #undef PROP
 15098     }
 15099 }
 15100 
 15101 
 15102 /* Display one line of the tool-bar of frame IT->f.
 15103 
 15104    HEIGHT specifies the desired height of the tool-bar line.
 15105    If the actual height of the glyph row is less than HEIGHT, the
 15106    row's height is increased to HEIGHT, and the icons are centered
 15107    vertically in the new height.
 15108 
 15109    If HEIGHT is -1, we are counting needed tool-bar lines, so don't
 15110    count a final empty row in case the tool-bar width exactly matches
 15111    the window width.
 15112 */
 15113 
 15114 static void
 15115 display_tool_bar_line (struct it *it, int height)
 15116 {
 15117   struct glyph_row *row = it->glyph_row;
 15118   int max_x = it->last_visible_x;
 15119   struct glyph *last;
 15120 
 15121   /* Don't extend on a previously drawn tool bar items (Bug#16058).  */
 15122   clear_glyph_row (row);
 15123   row->enabled_p = true;
 15124   row->y = it->current_y;
 15125 
 15126   /* Note that this isn't made use of if the face hasn't a box,
 15127      so there's no need to check the face here.  */
 15128   it->start_of_box_run_p = true;
 15129 
 15130   while (it->current_x < max_x)
 15131     {
 15132       int x, n_glyphs_before, i, nglyphs;
 15133       struct it it_before;
 15134 
 15135       /* Get the next display element.  */
 15136       if (!get_next_display_element (it))
 15137         {
 15138           /* Don't count empty row if we are counting needed tool-bar lines.  */
 15139           if (height < 0 && !it->hpos)
 15140             return;
 15141           break;
 15142         }
 15143 
 15144       /* Produce glyphs.  */
 15145       n_glyphs_before = row->used[TEXT_AREA];
 15146       it_before = *it;
 15147 
 15148       PRODUCE_GLYPHS (it);
 15149 
 15150       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 15151       i = 0;
 15152       x = it_before.current_x;
 15153       while (i < nglyphs)
 15154         {
 15155           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 15156 
 15157           if (x + glyph->pixel_width > max_x)
 15158             {
 15159               /* Glyph doesn't fit on line.  Backtrack.  */
 15160               row->used[TEXT_AREA] = n_glyphs_before;
 15161               *it = it_before;
 15162               /* If this is the only glyph on this line, it will never fit on the
 15163                  tool-bar, so skip it.  But ensure there is at least one glyph,
 15164                  so we don't accidentally disable the tool-bar.  */
 15165               if (n_glyphs_before == 0
 15166                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 15167                 break;
 15168               goto out;
 15169             }
 15170 
 15171           ++it->hpos;
 15172           x += glyph->pixel_width;
 15173           ++i;
 15174         }
 15175 
 15176       /* Stop at line end.  */
 15177       if (ITERATOR_AT_END_OF_LINE_P (it))
 15178         break;
 15179 
 15180       set_iterator_to_next (it, true);
 15181     }
 15182 
 15183  out:;
 15184 
 15185   row->displays_text_p = row->used[TEXT_AREA] != 0;
 15186 
 15187   /* Use default face for the border below the tool bar.
 15188 
 15189      FIXME: When auto-resize-tool-bars is grow-only, there is
 15190      no additional border below the possibly empty tool-bar lines.
 15191      So to make the extra empty lines look "normal", we have to
 15192      use the tool-bar face for the border too.  */
 15193   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15194       && !EQ (Vauto_resize_tool_bars, Qgrow_only))
 15195     it->face_id = DEFAULT_FACE_ID;
 15196 
 15197   extend_face_to_end_of_line (it);
 15198   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 15199   last->right_box_line_p = true;
 15200   if (last == row->glyphs[TEXT_AREA])
 15201     last->left_box_line_p = true;
 15202 
 15203   /* Make line the desired height and center it vertically.  */
 15204   if ((height -= it->max_ascent + it->max_descent) > 0)
 15205     {
 15206       /* Don't add more than one line height.  */
 15207       height %= FRAME_LINE_HEIGHT (it->f);
 15208       it->max_ascent += height / 2;
 15209       it->max_descent += (height + 1) / 2;
 15210     }
 15211 
 15212   compute_line_metrics (it);
 15213 
 15214   /* If line is empty, make it occupy the rest of the tool-bar.  */
 15215   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 15216     {
 15217       row->height = row->phys_height = it->last_visible_y - row->y;
 15218       row->visible_height = row->height;
 15219       row->ascent = row->phys_ascent = 0;
 15220       row->extra_line_spacing = 0;
 15221     }
 15222 
 15223   row->full_width_p = true;
 15224   row->continued_p = false;
 15225   row->truncated_on_left_p = false;
 15226   row->truncated_on_right_p = false;
 15227 
 15228   it->current_x = it->hpos = 0;
 15229   it->current_y += row->height;
 15230   ++it->vpos;
 15231   ++it->glyph_row;
 15232 }
 15233 
 15234 
 15235 /* Value is the number of pixels needed to make all tool-bar items of
 15236    frame F visible.  The actual number of glyph rows needed is
 15237    returned in *N_ROWS if non-NULL.  */
 15238 static int
 15239 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 15240 {
 15241   struct window *w = XWINDOW (f->tool_bar_window);
 15242   struct it it;
 15243   /* tool_bar_height is called from redisplay_tool_bar after building
 15244      the desired matrix, so use (unused) mode-line row as temporary row to
 15245      avoid destroying the first tool-bar row.  */
 15246   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 15247 
 15248   /* Initialize an iterator for iteration over
 15249      F->desired_tool_bar_string in the tool-bar window of frame F.  */
 15250   init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
 15251   temp_row->reversed_p = false;
 15252   it.first_visible_x = 0;
 15253   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15254   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15255                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15256   it.paragraph_embedding = L2R;
 15257 
 15258   while (!ITERATOR_AT_END_P (&it))
 15259     {
 15260       clear_glyph_row (temp_row);
 15261       it.glyph_row = temp_row;
 15262       display_tool_bar_line (&it, -1);
 15263     }
 15264   clear_glyph_row (temp_row);
 15265 
 15266   /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar.  */
 15267   if (n_rows)
 15268     *n_rows = it.vpos > 0 ? it.vpos : -1;
 15269 
 15270   if (pixelwise)
 15271     return it.current_y;
 15272   else
 15273     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 15274 }
 15275 
 15276 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15277 
 15278 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
 15279        0, 2, 0,
 15280        doc: /* Return the number of lines occupied by the tool bar of FRAME.
 15281 If FRAME is nil or omitted, use the selected frame.  Optional argument
 15282 PIXELWISE non-nil means return the height of the tool bar in pixels.  */)
 15283   (Lisp_Object frame, Lisp_Object pixelwise)
 15284 {
 15285   int height = 0;
 15286 
 15287 #ifndef HAVE_EXT_TOOL_BAR
 15288   struct frame *f = decode_any_frame (frame);
 15289 
 15290   if (WINDOWP (f->tool_bar_window)
 15291       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
 15292     {
 15293       update_tool_bar (f, true);
 15294       if (f->n_tool_bar_items)
 15295         {
 15296           build_desired_tool_bar_string (f);
 15297           height = tool_bar_height (f, NULL, !NILP (pixelwise));
 15298         }
 15299     }
 15300 #endif
 15301 
 15302   return make_fixnum (height);
 15303 }
 15304 
 15305 #ifndef HAVE_EXT_TOOL_BAR
 15306 
 15307 /* Display the internal tool-bar of frame F.  Value is true if
 15308    tool-bar's height should be changed.  */
 15309 static bool
 15310 redisplay_tool_bar (struct frame *f)
 15311 {
 15312   struct window *w;
 15313   struct it it;
 15314   struct glyph_row *row;
 15315 
 15316   f->tool_bar_redisplayed = true;
 15317 
 15318   /* If frame hasn't a tool-bar window or if it is zero-height, don't
 15319      do anything.  This means you must start with tool-bar-lines
 15320      non-zero to get the auto-sizing effect.  Or in other words, you
 15321      can turn off tool-bars by specifying tool-bar-lines zero.  */
 15322   if (!WINDOWP (f->tool_bar_window)
 15323       || (w = XWINDOW (f->tool_bar_window),
 15324           WINDOW_TOTAL_LINES (w) == 0))
 15325     {
 15326       /* Even if we do not display a tool bar initially, still pretend
 15327          that we have resized it already.  This avoids that a later
 15328          activation of the tool bar resizes the frame, despite of the
 15329          fact that a setting of 'frame-inhibit-implied-resize' should
 15330          inhibit it (Bug#52986).  */
 15331       f->tool_bar_resized = true;
 15332 
 15333       return false;
 15334     }
 15335 
 15336   /* Set up an iterator for the tool-bar window.  */
 15337   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
 15338   it.first_visible_x = 0;
 15339   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15340   row = it.glyph_row;
 15341   row->reversed_p = false;
 15342 
 15343   /* Build a string that represents the contents of the tool-bar.  */
 15344   build_desired_tool_bar_string (f);
 15345   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15346                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15347   /* FIXME: This should be controlled by a user option.  But it
 15348      doesn't make sense to have an R2L tool bar if the menu bar cannot
 15349      be drawn also R2L, and making the menu bar R2L is tricky due to
 15350      toolkit-specific code that implements it.  If an R2L tool bar is
 15351      ever supported, display_tool_bar_line should also be augmented to
 15352      call unproduce_glyphs like display_line and display_string
 15353      do.  */
 15354   it.paragraph_embedding = L2R;
 15355 
 15356   if (f->n_tool_bar_rows == 0)
 15357     {
 15358       int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
 15359 
 15360       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 15361         {
 15362           if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15363             FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15364           frame_default_tool_bar_height = new_height;
 15365           /* Always do that now.  */
 15366           clear_glyph_matrix (w->desired_matrix);
 15367           f->fonts_changed = true;
 15368           return true;
 15369         }
 15370     }
 15371 
 15372   /* Display as many lines as needed to display all tool-bar items.  */
 15373 
 15374   if (f->n_tool_bar_rows > 0)
 15375     {
 15376       int border, rows, height, extra;
 15377 
 15378       if (TYPE_RANGED_FIXNUMP (int, Vtool_bar_border))
 15379         border = XFIXNUM (Vtool_bar_border);
 15380       else if (EQ (Vtool_bar_border, Qinternal_border_width))
 15381         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 15382       else if (EQ (Vtool_bar_border, Qborder_width))
 15383         border = f->border_width;
 15384       else
 15385         border = 0;
 15386       if (border < 0)
 15387         border = 0;
 15388 
 15389       rows = f->n_tool_bar_rows;
 15390       height = max (1, (it.last_visible_y - border) / rows);
 15391       extra = it.last_visible_y - border - height * rows;
 15392 
 15393       while (it.current_y < it.last_visible_y)
 15394         {
 15395           int h = 0;
 15396           if (extra > 0 && rows-- > 0)
 15397             {
 15398               h = (extra + rows - 1) / rows;
 15399               extra -= h;
 15400             }
 15401           display_tool_bar_line (&it, height + h);
 15402         }
 15403     }
 15404   else
 15405     {
 15406       while (it.current_y < it.last_visible_y)
 15407         display_tool_bar_line (&it, 0);
 15408     }
 15409 
 15410   /* It doesn't make much sense to try scrolling in the tool-bar
 15411      window, so don't do it.  */
 15412   w->desired_matrix->no_scrolling_p = true;
 15413   w->must_be_updated_p = true;
 15414 
 15415   if (!NILP (Vauto_resize_tool_bars))
 15416     {
 15417       bool change_height_p = false;
 15418 
 15419       /* If we couldn't display everything, change the tool-bar's
 15420          height if there is room for more.  */
 15421       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 15422         change_height_p = true;
 15423 
 15424       /* We subtract 1 because display_tool_bar_line advances the
 15425          glyph_row pointer before returning to its caller.  We want to
 15426          examine the last glyph row produced by
 15427          display_tool_bar_line.  */
 15428       row = it.glyph_row - 1;
 15429 
 15430       /* If there are blank lines at the end, except for a partially
 15431          visible blank line at the end that is smaller than
 15432          FRAME_LINE_HEIGHT, change the tool-bar's height.  */
 15433       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15434           && row->height >= FRAME_LINE_HEIGHT (f))
 15435         change_height_p = true;
 15436 
 15437       /* If row displays tool-bar items, but is partially visible,
 15438          change the tool-bar's height.  */
 15439       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15440           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 15441         change_height_p = true;
 15442 
 15443       /* Resize windows as needed by changing the `tool-bar-lines'
 15444          frame parameter.  */
 15445       if (change_height_p)
 15446         {
 15447           int nrows;
 15448           int new_height = tool_bar_height (f, &nrows, true);
 15449 
 15450           change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
 15451                               && !f->minimize_tool_bar_window_p)
 15452                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 15453                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 15454           f->minimize_tool_bar_window_p = false;
 15455 
 15456           if (change_height_p)
 15457             {
 15458               if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15459                 FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15460               frame_default_tool_bar_height = new_height;
 15461               clear_glyph_matrix (w->desired_matrix);
 15462               f->n_tool_bar_rows = nrows;
 15463               f->fonts_changed = true;
 15464 
 15465               return true;
 15466             }
 15467         }
 15468     }
 15469 
 15470   f->minimize_tool_bar_window_p = false;
 15471 
 15472   return false;
 15473 }
 15474 
 15475 /* Get information about the tool-bar item which is displayed in GLYPH
 15476    on frame F.  Return in *PROP_IDX the index where tool-bar item
 15477    properties start in F->tool_bar_items.  Value is false if
 15478    GLYPH doesn't display a tool-bar item.  */
 15479 
 15480 static bool
 15481 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
 15482 {
 15483   Lisp_Object prop;
 15484   ptrdiff_t charpos;
 15485 
 15486   /* This function can be called asynchronously, which means we must
 15487      exclude any possibility that Fget_text_property signals an
 15488      error.  */
 15489   charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
 15490   charpos = max (0, charpos);
 15491 
 15492   /* Get the text property `menu-item' at pos. The value of that
 15493      property is the start index of this item's properties in
 15494      F->tool_bar_items.  */
 15495   prop = Fget_text_property (make_fixnum (charpos),
 15496                              Qmenu_item, f->current_tool_bar_string);
 15497   if (! FIXNUMP (prop))
 15498     return false;
 15499   *prop_idx = XFIXNUM (prop);
 15500   return true;
 15501 }
 15502 
 15503 
 15504 /* Get information about the tool-bar item at position X/Y on frame F.
 15505    Return in *GLYPH a pointer to the glyph of the tool-bar item in
 15506    the current matrix of the tool-bar window of F, or NULL if not
 15507    on a tool-bar item.  Return in *PROP_IDX the index of the tool-bar
 15508    item in F->tool_bar_items.  Value is
 15509 
 15510    -1   if X/Y is not on a tool-bar item
 15511    0    if X/Y is on the same item that was highlighted before.
 15512    1    otherwise.  */
 15513 
 15514 static int
 15515 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 15516                    int *hpos, int *vpos, int *prop_idx)
 15517 {
 15518   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15519   struct window *w = XWINDOW (f->tool_bar_window);
 15520   int area;
 15521 
 15522   /* Find the glyph under X/Y.  */
 15523   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 15524   if (*glyph == NULL)
 15525     return -1;
 15526 
 15527   /* Get the start of this tool-bar item's properties in
 15528      f->tool_bar_items.  */
 15529   if (!tool_bar_item_info (f, *glyph, prop_idx))
 15530     return -1;
 15531 
 15532   /* Is mouse on the highlighted item?  */
 15533   if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
 15534       && *vpos >= hlinfo->mouse_face_beg_row
 15535       && *vpos <= hlinfo->mouse_face_end_row
 15536       && (*vpos > hlinfo->mouse_face_beg_row
 15537           || *hpos >= hlinfo->mouse_face_beg_col)
 15538       && (*vpos < hlinfo->mouse_face_end_row
 15539           || *hpos < hlinfo->mouse_face_end_col
 15540           || hlinfo->mouse_face_past_end))
 15541     return 0;
 15542 
 15543   return 1;
 15544 }
 15545 
 15546 
 15547 /* EXPORT:
 15548    Handle mouse button event on the tool-bar of frame F, at
 15549    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 15550    false for button release.  MODIFIERS is event modifiers for button
 15551    release.  DEVICE is the device the click came from, or Qt.  */
 15552 
 15553 void
 15554 handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p,
 15555                                    int modifiers, Lisp_Object device)
 15556 {
 15557   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15558   struct window *w = XWINDOW (f->tool_bar_window);
 15559   int hpos, vpos, prop_idx;
 15560   struct glyph *glyph;
 15561   Lisp_Object enabled_p;
 15562   int ts;
 15563 
 15564   /* If not on the highlighted tool-bar item, and mouse-highlight is
 15565      non-nil, return.  This is so we generate the tool-bar button
 15566      click only when the mouse button is released on the same item as
 15567      where it was pressed.  However, when mouse-highlight is disabled,
 15568      generate the click when the button is released regardless of the
 15569      highlight, since tool-bar items are not highlighted in that
 15570      case.  */
 15571   frame_to_window_pixel_xy (w, &x, &y);
 15572   ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15573   if (ts == -1
 15574       || (ts != 0 && !NILP (Vmouse_highlight)))
 15575     return;
 15576 
 15577   /* When mouse-highlight is off, generate the click for the item
 15578      where the button was pressed, disregarding where it was
 15579      released.  */
 15580   if (NILP (Vmouse_highlight) && !down_p)
 15581     prop_idx = f->last_tool_bar_item;
 15582 
 15583   /* If item is disabled, do nothing.  */
 15584   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15585   if (NILP (enabled_p))
 15586     return;
 15587 
 15588   if (down_p)
 15589     {
 15590       /* Show item in pressed state.  */
 15591       if (!NILP (Vmouse_highlight))
 15592         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 15593       f->last_tool_bar_item = prop_idx;
 15594     }
 15595   else
 15596     {
 15597       Lisp_Object key, frame;
 15598       struct input_event event;
 15599       EVENT_INIT (event);
 15600 
 15601       /* Show item in released state.  */
 15602       if (!NILP (Vmouse_highlight))
 15603         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 15604 
 15605       key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
 15606 
 15607       XSETFRAME (frame, f);
 15608       event.kind = TOOL_BAR_EVENT;
 15609       event.frame_or_window = frame;
 15610       event.arg = key;
 15611       event.modifiers = modifiers;
 15612       event.device = device;
 15613       kbd_buffer_store_event (&event);
 15614       f->last_tool_bar_item = -1;
 15615     }
 15616 }
 15617 
 15618 void
 15619 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
 15620                        int modifiers)
 15621 {
 15622   handle_tool_bar_click_with_device (f, x, y, down_p, modifiers, Qt);
 15623 }
 15624 
 15625 /* Possibly highlight a tool-bar item on frame F when mouse moves to
 15626    tool-bar window-relative coordinates X/Y.  Called from
 15627    note_mouse_highlight.  */
 15628 
 15629 static void
 15630 note_tool_bar_highlight (struct frame *f, int x, int y)
 15631 {
 15632   Lisp_Object window = f->tool_bar_window;
 15633   struct window *w = XWINDOW (window);
 15634   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 15635   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15636   int hpos, vpos;
 15637   struct glyph *glyph;
 15638   struct glyph_row *row;
 15639   int i;
 15640   Lisp_Object enabled_p;
 15641   int prop_idx;
 15642   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 15643   bool mouse_down_p;
 15644   int rc;
 15645 
 15646   /* Function note_mouse_highlight is called with negative X/Y
 15647      values when mouse moves outside of the frame.  */
 15648   if (x <= 0 || y <= 0)
 15649     {
 15650       clear_mouse_face (hlinfo);
 15651       return;
 15652     }
 15653 
 15654   rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15655   if (rc < 0)
 15656     {
 15657       /* Not on tool-bar item.  */
 15658       clear_mouse_face (hlinfo);
 15659       return;
 15660     }
 15661   else if (rc == 0)
 15662     /* On same tool-bar item as before.  */
 15663     goto set_help_echo;
 15664 
 15665   clear_mouse_face (hlinfo);
 15666 
 15667   /* Mouse is down, but on different tool-bar item?  */
 15668   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 15669                   && f == dpyinfo->last_mouse_frame);
 15670 
 15671   if (mouse_down_p && f->last_tool_bar_item != prop_idx)
 15672     return;
 15673 
 15674   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 15675 
 15676   /* If tool-bar item is not enabled, don't highlight it.  */
 15677   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15678   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 15679     {
 15680       /* Compute the x-position of the glyph.  In front and past the
 15681          image is a space.  We include this in the highlighted area.  */
 15682       row = MATRIX_ROW (w->current_matrix, vpos);
 15683       for (i = x = 0; i < hpos; ++i)
 15684         x += row->glyphs[TEXT_AREA][i].pixel_width;
 15685 
 15686       /* Record this as the current active region.  */
 15687       hlinfo->mouse_face_beg_col = hpos;
 15688       hlinfo->mouse_face_beg_row = vpos;
 15689       hlinfo->mouse_face_beg_x = x;
 15690       hlinfo->mouse_face_past_end = false;
 15691 
 15692       hlinfo->mouse_face_end_col = hpos + 1;
 15693       hlinfo->mouse_face_end_row = vpos;
 15694       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 15695       hlinfo->mouse_face_window = window;
 15696       hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
 15697 
 15698       /* Display it as active.  */
 15699       show_mouse_face (hlinfo, draw);
 15700     }
 15701 
 15702  set_help_echo:
 15703 
 15704   /* Set help_echo_string to a help string to display for this tool-bar item.
 15705      XTread_socket does the rest.  */
 15706   help_echo_object = help_echo_window = Qnil;
 15707   help_echo_pos = -1;
 15708   help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
 15709   if (NILP (help_echo_string))
 15710     help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
 15711 }
 15712 
 15713 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15714 
 15715 #endif /* HAVE_WINDOW_SYSTEM */
 15716 
 15717 
 15718 
 15719 /************************************************************************
 15720                          Horizontal scrolling
 15721  ************************************************************************/
 15722 
 15723 /* For all leaf windows in the window tree rooted at WINDOW, set their
 15724    hscroll value so that PT is (i) visible in the window, and (ii) so
 15725    that it is not within a certain margin at the window's left and
 15726    right border.  Value is true if any window's hscroll has been
 15727    changed.  */
 15728 
 15729 static bool
 15730 hscroll_window_tree (Lisp_Object window)
 15731 {
 15732   bool hscrolled_p = false;
 15733   bool hscroll_relative_p = FLOATP (Vhscroll_step);
 15734   int hscroll_step_abs = 0;
 15735   double hscroll_step_rel = 0;
 15736 
 15737   if (hscroll_relative_p)
 15738     {
 15739       hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
 15740       if (hscroll_step_rel < 0)
 15741         {
 15742           hscroll_relative_p = false;
 15743           hscroll_step_abs = 0;
 15744         }
 15745     }
 15746   else if (TYPE_RANGED_FIXNUMP (int, Vhscroll_step))
 15747     {
 15748       hscroll_step_abs = XFIXNUM (Vhscroll_step);
 15749       if (hscroll_step_abs < 0)
 15750         hscroll_step_abs = 0;
 15751     }
 15752   else
 15753     hscroll_step_abs = 0;
 15754 
 15755   while (WINDOWP (window))
 15756     {
 15757       struct window *w = XWINDOW (window);
 15758 
 15759       if (WINDOWP (w->contents))
 15760         hscrolled_p |= hscroll_window_tree (w->contents);
 15761       else if (w->cursor.vpos >= 0
 15762                /* Don't allow hscroll in mini-windows that display
 15763                   echo-area messages.  This is because desired_matrix
 15764                   of such windows was prepared while momentarily
 15765                   switched to an echo-area buffer, which is different
 15766                   from w->contents, and we simply cannot hscroll such
 15767                   windows safely.  */
 15768                && !(w == XWINDOW (echo_area_window)
 15769                     && !NILP (echo_area_buffer[0])))
 15770         {
 15771           int h_margin;
 15772           int text_area_width;
 15773           struct glyph_row *cursor_row;
 15774           struct glyph_row *bottom_row;
 15775 
 15776           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
 15777           if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
 15778             cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
 15779           else
 15780             cursor_row = bottom_row - 1;
 15781 
 15782           if (!cursor_row->enabled_p)
 15783             {
 15784               bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 15785               if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
 15786                 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 15787               else
 15788                 cursor_row = bottom_row - 1;
 15789             }
 15790           bool row_r2l_p = cursor_row->reversed_p;
 15791           bool hscl = hscrolling_current_line_p (w);
 15792           int x_offset = 0;
 15793           /* When line numbers are displayed, we need to account for
 15794              the horizontal space they consume.  */
 15795           if (!NILP (Vdisplay_line_numbers))
 15796             {
 15797               struct glyph *g;
 15798               if (!row_r2l_p)
 15799                 {
 15800                   for (g = cursor_row->glyphs[TEXT_AREA];
 15801                        g < cursor_row->glyphs[TEXT_AREA]
 15802                          + cursor_row->used[TEXT_AREA];
 15803                        g++)
 15804                     {
 15805                       if (!(NILP (g->object) && g->charpos < 0))
 15806                         break;
 15807                       x_offset += g->pixel_width;
 15808                     }
 15809                 }
 15810               else
 15811                 {
 15812                   for (g = cursor_row->glyphs[TEXT_AREA]
 15813                          + cursor_row->used[TEXT_AREA];
 15814                        g > cursor_row->glyphs[TEXT_AREA];
 15815                        g--)
 15816                     {
 15817                       if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
 15818                         break;
 15819                       x_offset += (g - 1)->pixel_width;
 15820                     }
 15821                 }
 15822             }
 15823           if (cursor_row->truncated_on_left_p)
 15824             {
 15825               /* On TTY frames, don't count the left truncation glyph.  */
 15826               struct frame *f = XFRAME (WINDOW_FRAME (w));
 15827               x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
 15828             }
 15829 
 15830           text_area_width = window_box_width (w, TEXT_AREA);
 15831 
 15832           /* Scroll when cursor is inside this scroll margin.  */
 15833           h_margin = (clip_to_bounds (0, hscroll_margin, 1000000)
 15834                       * WINDOW_FRAME_COLUMN_WIDTH (w));
 15835 
 15836           /* If the position of this window's point has explicitly
 15837              changed, no more suspend auto hscrolling.  */
 15838           if (w->suspend_auto_hscroll
 15839               && NILP (Fequal (Fwindow_point (window),
 15840                                Fwindow_old_point (window))))
 15841             {
 15842               w->suspend_auto_hscroll = false;
 15843               /* When hscrolling just the current line, and the rest
 15844                  of lines were temporarily hscrolled, but no longer
 15845                  are, force thorough redisplay of this window, to show
 15846                  the effect of disabling hscroll suspension immediately.  */
 15847               if (w->min_hscroll == 0 && w->hscroll > 0
 15848                   && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
 15849                          Qcurrent_line))
 15850                 SET_FRAME_GARBAGED (XFRAME (w->frame));
 15851             }
 15852 
 15853           /* Remember window point.  */
 15854           Fset_marker (w->old_pointm,
 15855                        ((w == XWINDOW (selected_window))
 15856                         ? make_fixnum (BUF_PT (XBUFFER (w->contents)))
 15857                         : Fmarker_position (w->pointm)),
 15858                        w->contents);
 15859 
 15860           if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
 15861               && !w->suspend_auto_hscroll
 15862               /* In some pathological cases, like restoring a window
 15863                  configuration into a frame that is much smaller than
 15864                  the one from which the configuration was saved, we
 15865                  get glyph rows whose start and end have zero buffer
 15866                  positions, which we cannot handle below.  Just skip
 15867                  such windows.  */
 15868               && (CHARPOS (cursor_row->start.pos)
 15869                   >= BUF_BEG (XBUFFER (w->contents)))
 15870               /* For left-to-right rows, hscroll when cursor is either
 15871                  (i) inside the right hscroll margin, or (ii) if it is
 15872                  inside the left margin and the window is already
 15873                  hscrolled.  */
 15874               && ((!row_r2l_p
 15875                    && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
 15876                        || (cursor_row->enabled_p
 15877                            && cursor_row->truncated_on_right_p
 15878                            && (w->cursor.x >= text_area_width - h_margin))))
 15879                   /* For right-to-left rows, the logic is similar,
 15880                      except that rules for scrolling to left and right
 15881                      are reversed.  E.g., if cursor.x <= h_margin, we
 15882                      need to hscroll "to the right" unconditionally,
 15883                      and that will scroll the screen to the left so as
 15884                      to reveal the next portion of the row.  */
 15885                   || (row_r2l_p
 15886                       && ((cursor_row->enabled_p
 15887                            /* FIXME: It is confusing to set the
 15888                               truncated_on_right_p flag when R2L rows
 15889                               are actually truncated on the left.  */
 15890                            && cursor_row->truncated_on_right_p
 15891                            && w->cursor.x <= h_margin)
 15892                           || (w->hscroll
 15893                               && (w->cursor.x >= (text_area_width - h_margin
 15894                                                   - x_offset)))))
 15895                   /* This last condition is needed when moving
 15896                      vertically from an hscrolled line to a short line
 15897                      that doesn't need to be hscrolled.  If we omit
 15898                      this condition, the line from which we move will
 15899                      remain hscrolled.  */
 15900                   || (hscl
 15901                       && w->hscroll != w->min_hscroll
 15902                       && !cursor_row->truncated_on_left_p)))
 15903             {
 15904               struct it it;
 15905               ptrdiff_t hscroll;
 15906               struct buffer *saved_current_buffer;
 15907               ptrdiff_t pt;
 15908               int wanted_x;
 15909 
 15910               /* Find point in a display of infinite width.  */
 15911               saved_current_buffer = current_buffer;
 15912               current_buffer = XBUFFER (w->contents);
 15913 
 15914               if (w == XWINDOW (selected_window))
 15915                 pt = PT;
 15916               else
 15917                 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
 15918 
 15919               /* Move iterator to pt starting at cursor_row->start in
 15920                  a line with infinite width.  */
 15921               init_to_row_start (&it, w, cursor_row);
 15922               if (hscl)
 15923                 it.first_visible_x = window_hscroll_limited (w, it.f)
 15924                                      * FRAME_COLUMN_WIDTH (it.f);
 15925               it.last_visible_x = DISP_INFINITY;
 15926 
 15927               ptrdiff_t nchars = pt - IT_CHARPOS (it);
 15928               if (current_buffer->long_line_optimizations_p
 15929                   && nchars > large_hscroll_threshold)
 15930                 {
 15931                   /* Special optimization for very long and truncated
 15932                      lines which need to be hscrolled far to the left:
 15933                      jump directly to the (approximate) first position
 15934                      that is visible, instead of slowly walking there.  */
 15935                   fast_move_it_horizontally (&it, nchars);
 15936                   it.current_x += nchars * FRAME_COLUMN_WIDTH (it.f);
 15937                 }
 15938               else
 15939                 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
 15940               /* If the line ends in an overlay string with a newline,
 15941                  we might infloop, because displaying the window will
 15942                  want to put the cursor after the overlay, i.e. at X
 15943                  coordinate of zero on the next screen line.  So we
 15944                  use the buffer position prior to the overlay string
 15945                  instead.  */
 15946               if (it.method == GET_FROM_STRING && pt > 1)
 15947                 {
 15948                   init_to_row_start (&it, w, cursor_row);
 15949                   if (hscl)
 15950                     it.first_visible_x = (window_hscroll_limited (w, it.f)
 15951                                           * FRAME_COLUMN_WIDTH (it.f));
 15952                   if (current_buffer->long_line_optimizations_p
 15953                       && nchars > large_hscroll_threshold)
 15954                     {
 15955                       fast_move_it_horizontally (&it, nchars - 1);
 15956                       it.current_x += (nchars - 1) * FRAME_COLUMN_WIDTH (it.f);
 15957                     }
 15958                   else
 15959                     move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
 15960                 }
 15961               current_buffer = saved_current_buffer;
 15962 
 15963               /* Position cursor in window.  */
 15964               if (!hscroll_relative_p && hscroll_step_abs == 0)
 15965                 hscroll = max (0, (it.current_x
 15966                                    - (ITERATOR_AT_END_OF_LINE_P (&it)
 15967                                       ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
 15968                                       : (text_area_width / 2))))
 15969                           / FRAME_COLUMN_WIDTH (it.f);
 15970               else if ((!row_r2l_p
 15971                         && w->cursor.x >= text_area_width - h_margin)
 15972                        || (row_r2l_p && w->cursor.x <= h_margin))
 15973                 {
 15974                   if (hscroll_relative_p)
 15975                     wanted_x = text_area_width * (1 - hscroll_step_rel)
 15976                                - h_margin;
 15977                   else
 15978                     wanted_x = text_area_width
 15979                                - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 15980                                - h_margin;
 15981                   hscroll
 15982                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 15983                 }
 15984               else
 15985                 {
 15986                   if (hscroll_relative_p)
 15987                     wanted_x =
 15988                       text_area_width * hscroll_step_rel + h_margin + x_offset;
 15989                   else
 15990                     wanted_x =
 15991                       hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 15992                       + h_margin + x_offset;
 15993                   hscroll
 15994                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 15995                 }
 15996               hscroll = max (hscroll, w->min_hscroll);
 15997 
 15998               /* Don't prevent redisplay optimizations if hscroll
 15999                  hasn't changed, as it will unnecessarily slow down
 16000                  redisplay.  */
 16001               if (w->hscroll != hscroll
 16002                   /* When hscrolling only the current line, we need to
 16003                      report hscroll even if its value is equal to the
 16004                      previous one, because the new line might need a
 16005                      different value.  */
 16006                   || (hscl && w->last_cursor_vpos != w->cursor.vpos))
 16007                 {
 16008                   struct buffer *b = XBUFFER (w->contents);
 16009                   b->prevent_redisplay_optimizations_p = true;
 16010                   w->hscroll = hscroll;
 16011                   hscrolled_p = true;
 16012                 }
 16013             }
 16014         }
 16015 
 16016       window = w->next;
 16017     }
 16018 
 16019   /* Value is true if hscroll of any leaf window has been changed.  */
 16020   return hscrolled_p;
 16021 }
 16022 
 16023 
 16024 /* Set hscroll so that cursor is visible and not inside horizontal
 16025    scroll margins for all windows in the tree rooted at WINDOW.  See
 16026    also hscroll_window_tree above.  Value is true if any window's
 16027    hscroll has been changed.  If it has, desired matrices on the frame
 16028    of WINDOW are cleared.  */
 16029 
 16030 static bool
 16031 hscroll_windows (Lisp_Object window)
 16032 {
 16033   bool hscrolled_p = hscroll_window_tree (window);
 16034   if (hscrolled_p)
 16035     clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
 16036   return hscrolled_p;
 16037 }
 16038 
 16039 
 16040 
 16041 /************************************************************************
 16042                                 Redisplay
 16043  ************************************************************************/
 16044 
 16045 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
 16046    This is sometimes handy to have in a debugger session.  */
 16047 
 16048 #ifdef GLYPH_DEBUG
 16049 
 16050 /* First and last unchanged row for try_window_id.  */
 16051 
 16052 static int debug_first_unchanged_at_end_vpos;
 16053 static int debug_last_unchanged_at_beg_vpos;
 16054 
 16055 /* Delta vpos and y.  */
 16056 
 16057 static int debug_dvpos, debug_dy;
 16058 
 16059 /* Delta in characters and bytes for try_window_id.  */
 16060 
 16061 static ptrdiff_t debug_delta, debug_delta_bytes;
 16062 
 16063 /* Values of window_end_pos and window_end_vpos at the end of
 16064    try_window_id.  */
 16065 
 16066 static ptrdiff_t debug_end_vpos;
 16067 
 16068 /* Append a string to W->desired_matrix->method.  FMT is a printf
 16069    format string.  If trace_redisplay_p is true also printf the
 16070    resulting string to stderr.  */
 16071 
 16072 static void debug_method_add (struct window *, char const *, ...)
 16073   ATTRIBUTE_FORMAT_PRINTF (2, 3);
 16074 
 16075 static void
 16076 debug_method_add (struct window *w, char const *fmt, ...)
 16077 {
 16078   void *ptr = w;
 16079   char *method = w->desired_matrix->method;
 16080   int len = strlen (method);
 16081   int size = sizeof w->desired_matrix->method;
 16082   int remaining = size - len - 1;
 16083   va_list ap;
 16084 
 16085   if (len && remaining)
 16086     {
 16087       method[len] = '|';
 16088       --remaining, ++len;
 16089     }
 16090 
 16091   va_start (ap, fmt);
 16092   vsnprintf (method + len, remaining + 1, fmt, ap);
 16093   va_end (ap);
 16094 
 16095   if (trace_redisplay_p)
 16096     fprintf (stderr, "%p (%s): %s\n",
 16097              ptr,
 16098              ((BUFFERP (w->contents)
 16099                && STRINGP (BVAR (XBUFFER (w->contents), name)))
 16100               ? SSDATA (BVAR (XBUFFER (w->contents), name))
 16101               : "no buffer"),
 16102              method + len);
 16103 }
 16104 
 16105 #endif /* GLYPH_DEBUG */
 16106 
 16107 
 16108 /* Value is true if all changes in window W, which displays
 16109    current_buffer, are in the text between START and END.  START is a
 16110    buffer position, END is given as a distance from Z.  Used in
 16111    redisplay_internal for display optimization.  */
 16112 
 16113 static bool
 16114 text_outside_line_unchanged_p (struct window *w,
 16115                                ptrdiff_t start, ptrdiff_t end)
 16116 {
 16117   bool unchanged_p = true;
 16118 
 16119   /* If text or overlays have changed, see where.  */
 16120   if (window_outdated (w))
 16121     {
 16122       /* Gap in the line?  */
 16123       if (GPT < start || Z - GPT < end)
 16124         unchanged_p = false;
 16125 
 16126       /* Changes start in front of the line, or end after it?  */
 16127       if (unchanged_p
 16128           && (BEG_UNCHANGED < start - 1
 16129               || END_UNCHANGED < end))
 16130         unchanged_p = false;
 16131 
 16132       /* If selective display, can't optimize if changes start at the
 16133          beginning of the line.  */
 16134       if (unchanged_p
 16135           && FIXNUMP (BVAR (current_buffer, selective_display))
 16136           && XFIXNUM (BVAR (current_buffer, selective_display)) > 0
 16137           && (BEG_UNCHANGED < start || GPT <= start))
 16138         unchanged_p = false;
 16139 
 16140       /* If there are overlays at the start or end of the line, these
 16141          may have overlay strings with newlines in them.  A change at
 16142          START, for instance, may actually concern the display of such
 16143          overlay strings as well, and they are displayed on different
 16144          lines.  So, quickly rule out this case.  (For the future, it
 16145          might be desirable to implement something more telling than
 16146          just BEG/END_UNCHANGED.)  */
 16147       if (unchanged_p)
 16148         {
 16149           if (BEG + BEG_UNCHANGED == start
 16150               && overlay_touches_p (start))
 16151             unchanged_p = false;
 16152           if (END_UNCHANGED == end
 16153               && overlay_touches_p (Z - end))
 16154             unchanged_p = false;
 16155         }
 16156 
 16157       /* Under bidi reordering, adding or deleting a character in the
 16158          beginning of a paragraph, before the first strong directional
 16159          character, can change the base direction of the paragraph (unless
 16160          the buffer specifies a fixed paragraph direction), which will
 16161          require redisplaying the whole paragraph.  It might be worthwhile
 16162          to find the paragraph limits and widen the range of redisplayed
 16163          lines to that, but for now just give up this optimization.  */
 16164       if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 16165           && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 16166         unchanged_p = false;
 16167     }
 16168 
 16169   return unchanged_p;
 16170 }
 16171 
 16172 
 16173 /* Do a frame update, taking possible shortcuts into account.  This is
 16174    the main external entry point for redisplay.
 16175 
 16176    If the last redisplay displayed an echo area message and that message
 16177    is no longer requested, we clear the echo area or bring back the
 16178    mini-buffer if that is in use.  */
 16179 
 16180 void
 16181 redisplay (void)
 16182 {
 16183   redisplay_internal ();
 16184 }
 16185 
 16186 
 16187 static Lisp_Object
 16188 overlay_arrow_string_or_property (Lisp_Object var)
 16189 {
 16190   Lisp_Object val;
 16191 
 16192   if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
 16193     return val;
 16194 
 16195   return Voverlay_arrow_string;
 16196 }
 16197 
 16198 /* Return true if there are any overlay-arrows in current_buffer.  */
 16199 static bool
 16200 overlay_arrow_in_current_buffer_p (void)
 16201 {
 16202   Lisp_Object vlist;
 16203 
 16204   for (vlist = Voverlay_arrow_variable_list;
 16205        CONSP (vlist);
 16206        vlist = XCDR (vlist))
 16207     {
 16208       Lisp_Object var = XCAR (vlist);
 16209       Lisp_Object val;
 16210 
 16211       if (!SYMBOLP (var))
 16212         continue;
 16213       val = find_symbol_value (var);
 16214       if (MARKERP (val)
 16215           && current_buffer == XMARKER (val)->buffer)
 16216         return true;
 16217     }
 16218   return false;
 16219 }
 16220 
 16221 
 16222 /* Return true if any overlay_arrows have moved or overlay-arrow-string
 16223    has changed.
 16224    If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
 16225    buffers that are affected.  */
 16226 
 16227 static bool
 16228 overlay_arrows_changed_p (bool set_redisplay)
 16229 {
 16230   Lisp_Object vlist;
 16231   bool changed = false;
 16232 
 16233   for (vlist = Voverlay_arrow_variable_list;
 16234        CONSP (vlist);
 16235        vlist = XCDR (vlist))
 16236     {
 16237       Lisp_Object var = XCAR (vlist);
 16238       Lisp_Object val, pstr;
 16239 
 16240       if (!SYMBOLP (var))
 16241         continue;
 16242       val = find_symbol_value (var);
 16243       if (!MARKERP (val))
 16244         continue;
 16245       if (! EQ (Fmarker_position (val),
 16246                 /* FIXME: Don't we have a problem, using such a global
 16247                  * "last-position" if the variable is buffer-local?  */
 16248                 Fget (var, Qlast_arrow_position))
 16249           || ! (pstr = overlay_arrow_string_or_property (var),
 16250                 EQ (pstr, Fget (var, Qlast_arrow_string))))
 16251         {
 16252           struct buffer *buf = XMARKER (val)->buffer;
 16253 
 16254           if (set_redisplay)
 16255             {
 16256               if (buf)
 16257                 bset_redisplay (buf);
 16258               changed = true;
 16259             }
 16260           else
 16261             return true;
 16262         }
 16263     }
 16264   return changed;
 16265 }
 16266 
 16267 /* Mark overlay arrows to be updated on next redisplay.  */
 16268 
 16269 static void
 16270 update_overlay_arrows (int up_to_date)
 16271 {
 16272   Lisp_Object vlist;
 16273 
 16274   for (vlist = Voverlay_arrow_variable_list;
 16275        CONSP (vlist);
 16276        vlist = XCDR (vlist))
 16277     {
 16278       Lisp_Object var = XCAR (vlist);
 16279 
 16280       if (!SYMBOLP (var))
 16281         continue;
 16282 
 16283       if (up_to_date > 0)
 16284         {
 16285           Lisp_Object val = find_symbol_value (var);
 16286           if (!MARKERP (val))
 16287             continue;
 16288           Fput (var, Qlast_arrow_position, Fmarker_position (val));
 16289           Fput (var, Qlast_arrow_string,
 16290                 overlay_arrow_string_or_property (var));
 16291         }
 16292       else if (up_to_date < 0
 16293                || !NILP (Fget (var, Qlast_arrow_position)))
 16294         {
 16295           Fput (var, Qlast_arrow_position, Qt);
 16296           Fput (var, Qlast_arrow_string, Qt);
 16297         }
 16298     }
 16299 }
 16300 
 16301 
 16302 /* Return overlay arrow string to display at row.
 16303    Return integer (bitmap number) for arrow bitmap in left fringe.
 16304    Return nil if no overlay arrow.  */
 16305 
 16306 static Lisp_Object
 16307 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
 16308 {
 16309   Lisp_Object vlist;
 16310 
 16311   for (vlist = Voverlay_arrow_variable_list;
 16312        CONSP (vlist);
 16313        vlist = XCDR (vlist))
 16314     {
 16315       Lisp_Object var = XCAR (vlist);
 16316       Lisp_Object val;
 16317 
 16318       if (!SYMBOLP (var))
 16319         continue;
 16320 
 16321       val = find_symbol_value (var);
 16322 
 16323       if (MARKERP (val)
 16324           && current_buffer == XMARKER (val)->buffer
 16325           && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
 16326         {
 16327           if (FRAME_WINDOW_P (it->f)
 16328               /* FIXME: if ROW->reversed_p is set, this should test
 16329                  the right fringe, not the left one.  */
 16330               && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
 16331             {
 16332 #ifdef HAVE_WINDOW_SYSTEM
 16333               if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
 16334                 {
 16335                   int fringe_bitmap = lookup_fringe_bitmap (val);
 16336                   if (fringe_bitmap != 0)
 16337                     return make_fixnum (fringe_bitmap);
 16338                 }
 16339 #endif
 16340               return make_fixnum (-1); /* Use default arrow bitmap.  */
 16341             }
 16342           return overlay_arrow_string_or_property (var);
 16343         }
 16344     }
 16345 
 16346   return Qnil;
 16347 }
 16348 
 16349 /* Return true if point moved out of or into a composition.  Otherwise
 16350    return false.  PREV_BUF and PREV_PT are the last point buffer and
 16351    position.  BUF and PT are the current point buffer and position.  */
 16352 
 16353 static bool
 16354 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
 16355                             struct buffer *buf, ptrdiff_t pt)
 16356 {
 16357   ptrdiff_t start, end;
 16358   Lisp_Object prop;
 16359   Lisp_Object buffer;
 16360 
 16361   XSETBUFFER (buffer, buf);
 16362   /* Check a composition at the last point if point moved within the
 16363      same buffer.  */
 16364   if (prev_buf == buf)
 16365     {
 16366       if (prev_pt == pt)
 16367         /* Point didn't move.  */
 16368         return false;
 16369 
 16370       if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
 16371           && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
 16372           && composition_valid_p (start, end, prop)
 16373           && start < prev_pt && end > prev_pt)
 16374         /* The last point was within the composition.  Return true iff
 16375             point moved out of the composition.  */
 16376         return (pt <= start || pt >= end);
 16377     }
 16378 
 16379   /* Check a composition at the current point.  */
 16380   return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
 16381           && find_composition (pt, -1, &start, &end, &prop, buffer)
 16382           && composition_valid_p (start, end, prop)
 16383           && start < pt && end > pt);
 16384 }
 16385 
 16386 /* Reconsider the clip changes of buffer which is displayed in W.  */
 16387 
 16388 static void
 16389 reconsider_clip_changes (struct window *w)
 16390 {
 16391   struct buffer *b = XBUFFER (w->contents);
 16392 
 16393   if (b->clip_changed
 16394       && w->window_end_valid
 16395       && w->current_matrix->buffer == b
 16396       && w->current_matrix->zv == BUF_ZV (b)
 16397       && w->current_matrix->begv == BUF_BEGV (b))
 16398     b->clip_changed = false;
 16399 
 16400   /* If display wasn't paused, and W is not a tool bar window, see if
 16401      point has been moved into or out of a composition.  In that case,
 16402      set b->clip_changed to force updating the screen.  If
 16403      b->clip_changed has already been set, skip this check.  */
 16404   if (!b->clip_changed && w->window_end_valid)
 16405     {
 16406       ptrdiff_t pt = (w == XWINDOW (selected_window)
 16407                       ? PT : marker_position (w->pointm));
 16408 
 16409       if ((w->current_matrix->buffer != b || pt != w->last_point)
 16410           && check_point_in_composition (w->current_matrix->buffer,
 16411                                          w->last_point, b, pt))
 16412         b->clip_changed = true;
 16413     }
 16414 }
 16415 
 16416 static void
 16417 propagate_buffer_redisplay (void)
 16418 { /* Resetting b->text->redisplay is problematic!
 16419      We can't just reset it in the case that some window that displays
 16420      it has not been redisplayed; and such a window can stay
 16421      unredisplayed for a long time if it's currently invisible.
 16422      But we do want to reset it at the end of redisplay otherwise
 16423      its displayed windows will keep being redisplayed over and over
 16424      again.
 16425      So we copy all b->text->redisplay flags up to their windows here,
 16426      such that mark_window_display_accurate can safely reset
 16427      b->text->redisplay.  */
 16428   Lisp_Object ws = window_list ();
 16429   for (; CONSP (ws); ws = XCDR (ws))
 16430     {
 16431       struct window *thisw = XWINDOW (XCAR (ws));
 16432       struct buffer *thisb = XBUFFER (thisw->contents);
 16433       if (thisb->text->redisplay)
 16434         thisw->redisplay = true;
 16435     }
 16436 }
 16437 
 16438 #define STOP_POLLING                                    \
 16439 do { if (! polling_stopped_here) stop_polling ();       \
 16440        polling_stopped_here = true; } while (false)
 16441 
 16442 #define RESUME_POLLING                                  \
 16443 do { if (polling_stopped_here) start_polling ();        \
 16444        polling_stopped_here = false; } while (false)
 16445 
 16446 /* Perhaps in the future avoid recentering windows if it
 16447    is not necessary; currently that causes some problems.  */
 16448 
 16449 static void
 16450 redisplay_internal (void)
 16451 {
 16452   struct window *w = XWINDOW (selected_window);
 16453   struct window *sw;
 16454   struct frame *fr;
 16455   bool pending;
 16456   bool must_finish = false, match_p;
 16457   struct text_pos tlbufpos, tlendpos;
 16458   int number_of_visible_frames;
 16459   struct frame *sf;
 16460   bool polling_stopped_here = false;
 16461   Lisp_Object tail, frame;
 16462 
 16463   /* Set a limit to the number of retries we perform due to horizontal
 16464      scrolling, this avoids getting stuck in an uninterruptible
 16465      infinite loop (Bug #24633).  */
 16466   enum { MAX_HSCROLL_RETRIES = 16 };
 16467   int hscroll_retries = 0;
 16468 
 16469   /* Limit the number of retries for when frame(s) become garbaged as
 16470      result of redisplaying them.  Some packages set various redisplay
 16471      hooks, such as window-scroll-functions, to run Lisp that always
 16472      calls APIs which cause the frame's garbaged flag to become set,
 16473      so we loop indefinitely.  */
 16474   enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
 16475   int garbaged_frame_retries = 0;
 16476 
 16477   /* True means redisplay has to consider all windows on all
 16478      frames.  False, only selected_window is considered.  */
 16479   bool consider_all_windows_p;
 16480 
 16481   /* True means redisplay has to redisplay the miniwindow.  */
 16482   bool update_miniwindow_p = false;
 16483 
 16484   redisplay_trace ("redisplay_internal %d\n", redisplaying_p);
 16485 
 16486   /* No redisplay if running in batch mode or frame is not yet fully
 16487      initialized, or redisplay is explicitly turned off by setting
 16488      Vinhibit_redisplay.  */
 16489   if ((FRAME_INITIAL_P (SELECTED_FRAME ())
 16490        && redisplay_skip_initial_frame)
 16491       || !NILP (Vinhibit_redisplay))
 16492     return;
 16493 
 16494   /* Don't examine these until after testing Vinhibit_redisplay.
 16495      When Emacs is shutting down, perhaps because its connection to
 16496      X has dropped, we should not look at them at all.  */
 16497   fr = XFRAME (w->frame);
 16498   sf = SELECTED_FRAME ();
 16499 
 16500   if (!fr->glyphs_initialized_p)
 16501     return;
 16502 
 16503 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
 16504   if (popup_activated ())
 16505     return;
 16506 #endif
 16507 
 16508 #if defined (HAVE_HAIKU)
 16509   if (popup_activated_p)
 16510     return;
 16511 #endif
 16512 
 16513   /* I don't think this happens but let's be paranoid.  */
 16514   if (redisplaying_p)
 16515     return;
 16516 
 16517   /* Record a function that clears redisplaying_p
 16518      when we leave this function.  */
 16519   specpdl_ref count = SPECPDL_INDEX ();
 16520   record_unwind_protect_void (unwind_redisplay);
 16521   redisplaying_p = true;
 16522   block_buffer_flips ();
 16523   specbind (Qinhibit_free_realized_faces, Qnil);
 16524 
 16525   /* Record this function, so it appears on the profiler's backtraces.  */
 16526   record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
 16527 
 16528   FOR_EACH_FRAME (tail, frame)
 16529     XFRAME (frame)->already_hscrolled_p = false;
 16530 
 16531   reset_outermost_restrictions ();
 16532 
 16533  retry:
 16534   /* Remember the currently selected window.  */
 16535   sw = w;
 16536 
 16537   pending = false;
 16538   forget_escape_and_glyphless_faces ();
 16539 
 16540   inhibit_free_realized_faces = false;
 16541 
 16542   /* If face_change, init_iterator will free all realized faces, which
 16543      includes the faces referenced from current matrices.  So, we
 16544      can't reuse current matrices in this case.  */
 16545   if (face_change)
 16546     windows_or_buffers_changed = 47;
 16547 
 16548   if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
 16549       && FRAME_TTY (sf)->previous_frame != sf)
 16550     {
 16551       /* Since frames on a single ASCII terminal share the same
 16552          display area, displaying a different frame means redisplay
 16553          the whole thing.  */
 16554       SET_FRAME_GARBAGED (sf);
 16555 #ifndef DOS_NT
 16556       set_tty_color_mode (FRAME_TTY (sf), sf);
 16557 #endif
 16558       FRAME_TTY (sf)->previous_frame = sf;
 16559     }
 16560 
 16561   /* Set the visible flags for all frames.  Do this before checking for
 16562      resized or garbaged frames; they want to know if their frames are
 16563      visible.  See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
 16564   number_of_visible_frames = 0;
 16565 
 16566   FOR_EACH_FRAME (tail, frame)
 16567     {
 16568       struct frame *f = XFRAME (frame);
 16569 
 16570       if (FRAME_VISIBLE_P (f))
 16571         {
 16572           ++number_of_visible_frames;
 16573           /* Adjust matrices for visible frames only.  */
 16574           if (f->fonts_changed)
 16575             {
 16576               adjust_frame_glyphs (f);
 16577               /* Disable all redisplay optimizations for this frame.
 16578                  This is because adjust_frame_glyphs resets the
 16579                  enabled_p flag for all glyph rows of all windows, so
 16580                  many optimizations will fail anyway, and some might
 16581                  fail to test that flag and do bogus things as
 16582                  result.  */
 16583               SET_FRAME_GARBAGED (f);
 16584               f->fonts_changed = false;
 16585             }
 16586           /* If cursor type has been changed on the frame
 16587              other than selected, consider all frames.  */
 16588           if (f != sf && f->cursor_type_changed)
 16589             fset_redisplay (f);
 16590         }
 16591       clear_desired_matrices (f);
 16592     }
 16593 
 16594   /* Notice any pending interrupt request to change frame size.  */
 16595   do_pending_window_change (true);
 16596 
 16597   /* Clear frames marked as garbaged.  */
 16598   clear_garbaged_frames ();
 16599 
 16600   /* Build menubar and tool-bar items.  */
 16601   if (NILP (Vmemory_full))
 16602     prepare_menu_bars ();
 16603 
 16604   /* do_pending_window_change could change the selected_window due to
 16605      frame resizing which makes the selected window too small.
 16606      prepare_menu_bars may call lisp hooks and hence also change the
 16607      selected_window.  */
 16608   if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
 16609     sw = w;
 16610 
 16611   reconsider_clip_changes (w);
 16612 
 16613   /* In most cases selected window displays current buffer.  */
 16614   match_p = XBUFFER (w->contents) == current_buffer;
 16615   if (match_p)
 16616     {
 16617       /* Detect case that we need to write or remove a star in the mode line.  */
 16618       if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
 16619         w->update_mode_line = true;
 16620 
 16621       if (mode_line_update_needed (w))
 16622         w->update_mode_line = true;
 16623 
 16624       /* If reconsider_clip_changes above decided that the narrowing
 16625          in the current buffer changed, make sure all other windows
 16626          showing that buffer will be redisplayed.  */
 16627       if (current_buffer->clip_changed)
 16628         bset_update_mode_line (current_buffer);
 16629     }
 16630 
 16631   /* Normally the message* functions will have already displayed and
 16632      updated the echo area, but the frame may have been trashed, or
 16633      the update may have been preempted, so display the echo area
 16634      again here.  Checking message_cleared_p captures the case that
 16635      the echo area should be cleared.  */
 16636   if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
 16637       || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
 16638       || (message_cleared_p
 16639           && minibuf_level == 0
 16640           /* If the mini-window is currently selected, this means the
 16641              echo-area doesn't show through.  */
 16642           && !MINI_WINDOW_P (XWINDOW (selected_window))))
 16643     {
 16644       echo_area_display (false);
 16645 
 16646       if (message_cleared_p)
 16647         update_miniwindow_p = true;
 16648 
 16649       must_finish = true;
 16650 
 16651       /* If we don't display the current message, don't clear the
 16652          message_cleared_p flag, because, if we did, we wouldn't clear
 16653          the echo area in the next redisplay which doesn't preserve
 16654          the echo area.  */
 16655       if (!display_last_displayed_message_p)
 16656         message_cleared_p = false;
 16657     }
 16658   else if (EQ (selected_window, minibuf_window)
 16659            && (current_buffer->clip_changed || window_outdated (w))
 16660            && resize_mini_window (w, false))
 16661     {
 16662       /* Resized active mini-window to fit the size of what it is
 16663          showing if its contents might have changed.  */
 16664       must_finish = true;
 16665 
 16666       /* If window configuration was changed, frames may have been
 16667          marked garbaged.  Clear them or we will experience
 16668          surprises wrt scrolling.  */
 16669       clear_garbaged_frames ();
 16670     }
 16671 
 16672   if (!NILP (Vrun_hooks))
 16673     run_window_change_functions ();
 16674 
 16675   if (windows_or_buffers_changed && !update_mode_lines)
 16676     /* Code that sets windows_or_buffers_changed doesn't distinguish whether
 16677        only the windows's contents needs to be refreshed, or whether the
 16678        mode-lines also need a refresh.  */
 16679     update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
 16680                          ? REDISPLAY_SOME : 32);
 16681 
 16682   /* If specs for an arrow have changed, do thorough redisplay
 16683      to ensure we remove any arrow that should no longer exist.  */
 16684   /* Apparently, this is the only case where we update other windows,
 16685      without updating other mode-lines.  */
 16686   overlay_arrows_changed_p (true);
 16687 
 16688   consider_all_windows_p = (update_mode_lines
 16689                             || windows_or_buffers_changed);
 16690 
 16691 #define AINC(a,i)                                                       \
 16692   {                                                                     \
 16693     Lisp_Object entry = Fgethash (make_fixnum (i), a, make_fixnum (0)); \
 16694     if (FIXNUMP (entry))                                                \
 16695       Fputhash (make_fixnum (i), make_fixnum (1 + XFIXNUM (entry)), a); \
 16696   }
 16697 
 16698   AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
 16699   AINC (Vredisplay__mode_lines_cause, update_mode_lines);
 16700 
 16701   /* Optimize the case that only the line containing the cursor in the
 16702      selected window has changed.  Variables starting with this_ are
 16703      set in display_line and record information about the line
 16704      containing the cursor.  */
 16705   tlbufpos = this_line_start_pos;
 16706   tlendpos = this_line_end_pos;
 16707   if (!consider_all_windows_p
 16708       && CHARPOS (tlbufpos) > 0
 16709       && !w->update_mode_line
 16710       && !current_buffer->clip_changed
 16711       && !current_buffer->prevent_redisplay_optimizations_p
 16712       && FRAME_VISIBLE_P (XFRAME (w->frame))
 16713       && !FRAME_OBSCURED_P (XFRAME (w->frame))
 16714       && !XFRAME (w->frame)->cursor_type_changed
 16715       && !XFRAME (w->frame)->face_change
 16716       /* Make sure recorded data applies to current buffer, etc.  */
 16717       && this_line_buffer == current_buffer
 16718       && match_p
 16719       && !w->force_start
 16720       && !w->optional_new_start
 16721       /* Point must be on the line that we have info recorded about.  */
 16722       && PT >= CHARPOS (tlbufpos)
 16723       && PT <= Z - CHARPOS (tlendpos)
 16724       /* FIXME: The following condition is only needed when
 16725          significant parts of the buffer are hidden (e.g., under
 16726          hs-minor-mode), but there doesn't seem to be a simple way of
 16727          detecting that, so we always disable the one-line redisplay
 16728          optimizations whenever display-line-numbers-mode is turned on
 16729          in the buffer.  */
 16730       && (NILP (Vdisplay_line_numbers)
 16731           || EQ (Vdisplay_line_numbers, Qvisual))
 16732       /* All text outside that line, including its final newline,
 16733          must be unchanged.  */
 16734       && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
 16735                                         CHARPOS (tlendpos)))
 16736     {
 16737       if (CHARPOS (tlbufpos) > BEGV
 16738           && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
 16739           && (CHARPOS (tlbufpos) == ZV
 16740               || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
 16741         /* Former continuation line has disappeared by becoming empty.  */
 16742         goto cancel;
 16743       else if (window_outdated (w) || MINI_WINDOW_P (w))
 16744         {
 16745           /* We have to handle the case of continuation around a
 16746              wide-column character (see the comment in indent.c around
 16747              line 1340).
 16748 
 16749              For instance, in the following case:
 16750 
 16751              --------  Insert  --------
 16752              K_A_N_\\   `a'    K_A_N_a\         `X_' are wide-column chars.
 16753              J_I_       ==>    J_I_             `^^' are cursors.
 16754              ^^                ^^
 16755              --------          --------
 16756 
 16757              As we have to redraw the line above, we cannot use this
 16758              optimization.  */
 16759 
 16760           struct it it;
 16761           int line_height_before = this_line_pixel_height;
 16762 
 16763           /* Note that start_display will handle the case that the
 16764              line starting at tlbufpos is a continuation line.  */
 16765           start_display (&it, w, tlbufpos);
 16766 
 16767           /* Implementation note: It this still necessary?  */
 16768           if (it.current_x != this_line_start_x)
 16769             goto cancel;
 16770 
 16771           /* Give up on this optimization if the line starts with a
 16772              string with display property that draws on the fringes,
 16773              as that might interfere with line-prefix display.  */
 16774           if (it.sp > 1
 16775               && it.method == GET_FROM_IMAGE && it.image_id == -1)
 16776             goto cancel;
 16777           redisplay_trace ("trying display optimization 1\n");
 16778           w->cursor.vpos = -1;
 16779           overlay_arrow_seen = false;
 16780           it.vpos = this_line_vpos;
 16781           it.current_y = this_line_y;
 16782           it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
 16783           display_line (&it, -1);
 16784 
 16785           /* If line contains point, is not continued,
 16786              and ends at same distance from eob as before, we win.  */
 16787           if (w->cursor.vpos >= 0
 16788               /* Line is not continued, otherwise this_line_start_pos
 16789                  would have been set to 0 in display_line.  */
 16790               && CHARPOS (this_line_start_pos)
 16791               /* Line ends as before.  */
 16792               && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
 16793               /* Line has same height as before.  Otherwise other lines
 16794                  would have to be shifted up or down.  */
 16795               && this_line_pixel_height == line_height_before
 16796               /* Cannot use this optimization if hscrolling current
 16797                  line and this line is the current one, because
 16798                  display_line above is not informed about the
 16799                  current-line's vpos, and cannot DTRT in that case.  */
 16800               && !hscrolling_current_line_p (w))
 16801             {
 16802               /* If this is not the window's last line, we must adjust
 16803                  the charstarts of the lines below.  */
 16804               if (it.current_y < it.last_visible_y)
 16805                 {
 16806                   struct glyph_row *row
 16807                     = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
 16808                   ptrdiff_t delta, delta_bytes;
 16809 
 16810                   /* We used to distinguish between two cases here,
 16811                      conditioned by Z - CHARPOS (tlendpos) == ZV, for
 16812                      when the line ends in a newline or the end of the
 16813                      buffer's accessible portion.  But both cases did
 16814                      the same, so they were collapsed.  */
 16815                   delta = (Z
 16816                            - CHARPOS (tlendpos)
 16817                            - MATRIX_ROW_START_CHARPOS (row));
 16818                   delta_bytes = (Z_BYTE
 16819                                  - BYTEPOS (tlendpos)
 16820                                  - MATRIX_ROW_START_BYTEPOS (row));
 16821 
 16822                   increment_matrix_positions (w->current_matrix,
 16823                                               this_line_vpos + 1,
 16824                                               w->current_matrix->nrows,
 16825                                               delta, delta_bytes);
 16826                 }
 16827 
 16828               /* If this row displays text now but previously didn't,
 16829                  or vice versa, w->window_end_vpos may have to be
 16830                  adjusted.  */
 16831               if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
 16832                 {
 16833                   if (w->window_end_vpos < this_line_vpos)
 16834                     w->window_end_vpos = this_line_vpos;
 16835                 }
 16836               else if (w->window_end_vpos == this_line_vpos
 16837                        && this_line_vpos > 0)
 16838                 w->window_end_vpos = this_line_vpos - 1;
 16839               w->window_end_valid = false;
 16840 
 16841               /* Update hint: No need to try to scroll in update_window.  */
 16842               w->desired_matrix->no_scrolling_p = true;
 16843 
 16844 #ifdef GLYPH_DEBUG
 16845               *w->desired_matrix->method = 0;
 16846               debug_method_add (w, "optimization 1");
 16847 #endif
 16848 #ifdef HAVE_WINDOW_SYSTEM
 16849               update_window_fringes (w, false);
 16850 #endif
 16851               goto update;
 16852             }
 16853           else
 16854             goto cancel;
 16855         }
 16856       else if (/* Cursor position hasn't changed.  */
 16857                PT == w->last_point
 16858                /* Make sure the cursor was last displayed
 16859                   in this window.  Otherwise we have to reposition it.  */
 16860 
 16861                /* PXW: Must be converted to pixels, probably.  */
 16862                && 0 <= w->cursor.vpos
 16863                && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
 16864         {
 16865           if (!must_finish)
 16866             {
 16867               do_pending_window_change (true);
 16868               /* If selected_window changed, redisplay again.  */
 16869               if (WINDOWP (selected_window)
 16870                   && (w = XWINDOW (selected_window)) != sw)
 16871                 goto retry;
 16872 
 16873                 /* We used to always goto end_of_redisplay here, but this
 16874                  isn't enough if we have a blinking cursor.  */
 16875               if (w->cursor_off_p == w->last_cursor_off_p)
 16876                 goto end_of_redisplay;
 16877             }
 16878           goto update;
 16879         }
 16880       /* If highlighting the region, or if the cursor is in the echo area,
 16881          then we can't just move the cursor.  */
 16882       else if (NILP (Vshow_trailing_whitespace)
 16883                && !cursor_in_echo_area
 16884                && !composition_break_at_point)
 16885         {
 16886           struct it it;
 16887           struct glyph_row *row;
 16888 
 16889           /* Skip from tlbufpos to PT and see where it is.  Note that
 16890              PT may be in invisible text.  If so, we will end at the
 16891              next visible position.  */
 16892           init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
 16893                          NULL, DEFAULT_FACE_ID);
 16894           it.current_x = this_line_start_x;
 16895           it.current_y = this_line_y;
 16896           it.vpos = this_line_vpos;
 16897 
 16898           if (current_buffer->long_line_optimizations_p
 16899               && it.line_wrap == TRUNCATE
 16900               && PT - CHARPOS (tlbufpos) > large_hscroll_threshold)
 16901             {
 16902               /* When lines are very long and truncated, jumping to
 16903                  the next visible line is much faster than slowly
 16904                  iterating there.  */
 16905               reseat_at_next_visible_line_start (&it, false);
 16906               if (IT_CHARPOS (it) <= PT) /* point moved off this line */
 16907                 it.vpos = this_line_vpos + 1;
 16908             }
 16909           else
 16910             {
 16911               /* The call to move_it_to stops in front of PT, but
 16912                  moves over before-strings.  */
 16913               move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
 16914             }
 16915 
 16916           if (it.vpos == this_line_vpos
 16917               && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
 16918                   row->enabled_p))
 16919             {
 16920               eassert (this_line_vpos == it.vpos);
 16921               eassert (this_line_y == it.current_y);
 16922               set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 16923               if (cursor_row_fully_visible_p (w, false, true, false))
 16924                 {
 16925 #ifdef GLYPH_DEBUG
 16926                   *w->desired_matrix->method = 0;
 16927                   debug_method_add (w, "optimization 3");
 16928 #endif
 16929                   goto update;
 16930                 }
 16931               else
 16932                 goto cancel;
 16933             }
 16934           else
 16935             goto cancel;
 16936         }
 16937 
 16938     cancel:
 16939       /* Text changed drastically or point moved off of line.  */
 16940       SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
 16941     }
 16942 
 16943   CHARPOS (this_line_start_pos) = 0;
 16944   ++clear_face_cache_count;
 16945 #ifdef HAVE_WINDOW_SYSTEM
 16946   ++clear_image_cache_count;
 16947 #endif
 16948 
 16949   /* Build desired matrices, and update the display.  If
 16950      consider_all_windows_p, do it for all windows on all frames that
 16951      require redisplay, as specified by their 'redisplay' flag.
 16952      Otherwise do it for selected_window, only.  */
 16953 
 16954   if (consider_all_windows_p)
 16955     {
 16956       FOR_EACH_FRAME (tail, frame)
 16957         XFRAME (frame)->updated_p = false;
 16958 
 16959       propagate_buffer_redisplay ();
 16960 
 16961       FOR_EACH_FRAME (tail, frame)
 16962         {
 16963           struct frame *f = XFRAME (frame);
 16964 
 16965           /* We don't have to do anything for unselected terminal
 16966              frames.  */
 16967           if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
 16968               && !EQ (FRAME_TTY (f)->top_frame, frame))
 16969             continue;
 16970 
 16971         retry_frame:
 16972           if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
 16973             {
 16974               bool gcscrollbars
 16975                 /* Only GC scrollbars when we redisplay the whole frame.  */
 16976                 = f->redisplay || !REDISPLAY_SOME_P ();
 16977               bool f_redisplay_flag = f->redisplay;
 16978 
 16979               /* The X error handler may have deleted that frame
 16980                  before we went back to retry_frame.  This must come
 16981                  before any accesses to f->terminal.  */
 16982               if (!FRAME_LIVE_P (f))
 16983                 continue;
 16984 
 16985               /* Mark all the scroll bars to be removed; we'll redeem
 16986                  the ones we want when we redisplay their windows.  */
 16987               if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
 16988                 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
 16989 
 16990               if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
 16991                 {
 16992                   /* Don't allow freeing images and faces for this
 16993                      frame as long as the frame's update wasn't
 16994                      completed.  This prevents crashes when some Lisp
 16995                      that runs from the various hooks or font-lock
 16996                      decides to clear the frame's image cache and face
 16997                      cache, when the images and faces in those caches
 16998                      are referenced by the desired matrix.  */
 16999                   f->inhibit_clear_image_cache = true;
 17000                   redisplay_windows (FRAME_ROOT_WINDOW (f));
 17001                 }
 17002               /* Remember that the invisible frames need to be redisplayed next
 17003                  time they're visible.  */
 17004               else if (!REDISPLAY_SOME_P ())
 17005                 f->redisplay = true;
 17006 
 17007               /* The X error handler may have deleted that frame.  */
 17008               if (!FRAME_LIVE_P (f))
 17009                 continue;
 17010 
 17011               /* Any scroll bars which redisplay_windows should have
 17012                  nuked should now go away.  */
 17013               if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
 17014                 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
 17015 
 17016               if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
 17017                 {
 17018                   /* If fonts changed on visible frame, display again.  */
 17019                   if (f->fonts_changed)
 17020                     {
 17021                       adjust_frame_glyphs (f);
 17022                       /* Disable all redisplay optimizations for this
 17023                          frame.  For the reasons, see the comment near
 17024                          the previous call to adjust_frame_glyphs above.  */
 17025                       SET_FRAME_GARBAGED (f);
 17026                       f->fonts_changed = false;
 17027                       goto retry_frame;
 17028                     }
 17029 
 17030                   /* See if we have to hscroll.  */
 17031                   if (!f->already_hscrolled_p)
 17032                     {
 17033                       f->already_hscrolled_p = true;
 17034                       if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17035                           && hscroll_windows (f->root_window))
 17036                         {
 17037                           hscroll_retries++;
 17038                           goto retry_frame;
 17039                         }
 17040                     }
 17041 
 17042                   /* If the frame's redisplay flag was not set before
 17043                      we went about redisplaying its windows, but it is
 17044                      set now, that means we employed some redisplay
 17045                      optimizations inside redisplay_windows, and
 17046                      bypassed producing some screen lines.  But if
 17047                      f->redisplay is now set, it might mean the old
 17048                      faces are no longer valid (e.g., if redisplaying
 17049                      some window called some Lisp which defined a new
 17050                      face or redefined an existing face), so trying to
 17051                      use them in update_frame will segfault.
 17052                      Therefore, we must redisplay this frame.  */
 17053                   if (!f_redisplay_flag && f->redisplay)
 17054                     goto retry_frame;
 17055                   /* In some case (e.g., window resize), we notice
 17056                      only during window updating that the window
 17057                      content changed unpredictably (e.g., a GTK
 17058                      scrollbar moved, or some Lisp hook that winds up
 17059                      calling adjust_frame_glyphs) and that our
 17060                      previous estimation of the frame content was
 17061                      garbage.  We have to start over.  These cases
 17062                      should be rare, so going all the way back to the
 17063                      top of redisplay should be good enough.  */
 17064                   if (FRAME_GARBAGED_P (f)
 17065                       && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
 17066                     goto retry;
 17067 
 17068 #ifdef HAVE_WINDOW_SYSTEM
 17069                   if (FRAME_WINDOW_P (f)
 17070                       && FRAME_RIF (f)->clear_under_internal_border)
 17071                     FRAME_RIF (f)->clear_under_internal_border (f);
 17072 #endif
 17073                   /* Prevent various kinds of signals during display
 17074                      update.  stdio is not robust about handling
 17075                      signals, which can cause an apparent I/O error.  */
 17076                   if (interrupt_input)
 17077                     unrequest_sigio ();
 17078                   STOP_POLLING;
 17079 
 17080                   pending |= update_frame (f, false, false);
 17081                   /* On some platforms (at least MS-Windows), the
 17082                      scroll_run_hook called from scrolling_window
 17083                      called from update_frame could set the frame's
 17084                      garbaged flag, in which case we need to redisplay
 17085                      the frame.  Don't do that on TTY frames, since we
 17086                      need to keep the garbaged flag in that case when
 17087                      the frame has been resized.  */
 17088                   if (FRAME_GARBAGED_P (f))
 17089                     {
 17090                       fset_redisplay (f);
 17091                       f->garbaged = false;
 17092                       goto retry_frame;
 17093                     }
 17094                   f->cursor_type_changed = false;
 17095                   f->updated_p = true;
 17096                   f->inhibit_clear_image_cache = false;
 17097                 }
 17098             }
 17099         }
 17100 
 17101       eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
 17102 
 17103       if (!pending)
 17104         {
 17105           /* Do the mark_window_display_accurate after all windows have
 17106              been redisplayed because this call resets flags in buffers
 17107              which are needed for proper redisplay.  */
 17108           FOR_EACH_FRAME (tail, frame)
 17109             {
 17110               struct frame *f = XFRAME (frame);
 17111               if (f->updated_p)
 17112                 {
 17113                   f->redisplay = false;
 17114                   f->garbaged = false;
 17115                   mark_window_display_accurate (f->root_window, true);
 17116                   if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 17117                     FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
 17118                 }
 17119             }
 17120         }
 17121     }
 17122   else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
 17123     {
 17124       sf->inhibit_clear_image_cache = true;
 17125       displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
 17126       /* Use list_of_error, not Qerror, so that
 17127          we catch only errors and don't run the debugger.  */
 17128       internal_condition_case_1 (redisplay_window_1, selected_window,
 17129                                  list_of_error,
 17130                                  redisplay_window_error);
 17131       if (update_miniwindow_p)
 17132         {
 17133           Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17134 
 17135           displayed_buffer = XBUFFER (XWINDOW (mini_window)->contents);
 17136           internal_condition_case_1 (redisplay_window_1, mini_window,
 17137                                      list_of_error,
 17138                                      redisplay_window_error);
 17139         }
 17140 
 17141       /* Compare desired and current matrices, perform output.  */
 17142 
 17143     update:
 17144       /* If fonts changed, display again.  Likewise if redisplay_window_1
 17145          above caused some change (e.g., a change in faces) that requires
 17146          considering the entire frame again.  */
 17147       if (sf->fonts_changed || sf->redisplay)
 17148         {
 17149           if (sf->redisplay)
 17150             {
 17151               /* Set this to force a more thorough redisplay.
 17152                  Otherwise, we might immediately loop back to the
 17153                  above "else-if" clause (since all the conditions that
 17154                  led here might still be true), and we will then
 17155                  infloop, because the selected-frame's redisplay flag
 17156                  is not (and cannot be) reset.  */
 17157               windows_or_buffers_changed = 50;
 17158             }
 17159           goto retry;
 17160         }
 17161 
 17162       /* Prevent freeing of realized faces, since desired matrices are
 17163          pending that reference the faces we computed and cached.  */
 17164       inhibit_free_realized_faces = true;
 17165 
 17166       /* Prevent various kinds of signals during display update.
 17167          stdio is not robust about handling signals,
 17168          which can cause an apparent I/O error.  */
 17169       if (interrupt_input)
 17170         unrequest_sigio ();
 17171       STOP_POLLING;
 17172 
 17173       if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
 17174         {
 17175           if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17176               && hscroll_windows (selected_window))
 17177             {
 17178               hscroll_retries++;
 17179               goto retry;
 17180             }
 17181 
 17182           XWINDOW (selected_window)->must_be_updated_p = true;
 17183           pending = update_frame (sf, false, false);
 17184           sf->cursor_type_changed = false;
 17185           sf->inhibit_clear_image_cache = false;
 17186         }
 17187 
 17188       /* We may have called echo_area_display at the top of this
 17189          function.  If the echo area is on another frame, that may
 17190          have put text on a frame other than the selected one, so the
 17191          above call to update_frame would not have caught it.  Catch
 17192          it here.  */
 17193       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17194       struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 17195 
 17196       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
 17197         {
 17198           XWINDOW (mini_window)->must_be_updated_p = true;
 17199           pending |= update_frame (mini_frame, false, false);
 17200           mini_frame->cursor_type_changed = false;
 17201           if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
 17202               && hscroll_windows (mini_window))
 17203             {
 17204               hscroll_retries++;
 17205               goto retry;
 17206             }
 17207         }
 17208     }
 17209 
 17210   /* If display was paused because of pending input, make sure we do a
 17211      thorough update the next time.  */
 17212   if (pending)
 17213     {
 17214       /* Prevent the optimization at the beginning of
 17215          redisplay_internal that tries a single-line update of the
 17216          line containing the cursor in the selected window.  */
 17217       CHARPOS (this_line_start_pos) = 0;
 17218 
 17219       /* Let the overlay arrow be updated the next time.  */
 17220       update_overlay_arrows (0);
 17221 
 17222       /* If we pause after scrolling, some rows in the current
 17223          matrices of some windows are not valid.  */
 17224       if (!WINDOW_FULL_WIDTH_P (w)
 17225           && !FRAME_WINDOW_P (XFRAME (w->frame)))
 17226         update_mode_lines = 36;
 17227     }
 17228   else
 17229     {
 17230       if (!consider_all_windows_p)
 17231         {
 17232           /* This has already been done above if
 17233              consider_all_windows_p is set.  */
 17234           if (XBUFFER (w->contents)->text->redisplay
 17235               && buffer_window_count (XBUFFER (w->contents)) > 1)
 17236             /* This can happen if b->text->redisplay was set during
 17237                jit-lock.  */
 17238             propagate_buffer_redisplay ();
 17239           mark_window_display_accurate_1 (w, true);
 17240 
 17241           /* Say overlay arrows are up to date.  */
 17242           update_overlay_arrows (1);
 17243 
 17244           if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
 17245             FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
 17246         }
 17247 
 17248       update_mode_lines = 0;
 17249       windows_or_buffers_changed = 0;
 17250     }
 17251 
 17252   /* Start SIGIO interrupts coming again.  Having them off during the
 17253      code above makes it less likely one will discard output, but not
 17254      impossible, since there might be stuff in the system buffer here.
 17255      But it is much hairier to try to do anything about that.  */
 17256   if (interrupt_input)
 17257     request_sigio ();
 17258   RESUME_POLLING;
 17259 
 17260   /* If a frame has become visible which was not before, redisplay
 17261      again, so that we display it.  Expose events for such a frame
 17262      (which it gets when becoming visible) don't call the parts of
 17263      redisplay constructing glyphs, so simply exposing a frame won't
 17264      display anything in this case.  So, we have to display these
 17265      frames here explicitly.  */
 17266   if (!pending)
 17267     {
 17268       int new_count = 0;
 17269 
 17270       FOR_EACH_FRAME (tail, frame)
 17271         {
 17272           if (XFRAME (frame)->visible)
 17273             new_count++;
 17274         }
 17275 
 17276       if (new_count != number_of_visible_frames)
 17277         windows_or_buffers_changed = 52;
 17278     }
 17279 
 17280   /* Change frame size now if a change is pending.  */
 17281   do_pending_window_change (true);
 17282 
 17283   /* If we just did a pending size change, or have additional
 17284      visible frames, or selected_window changed, redisplay again.  */
 17285   if ((windows_or_buffers_changed && !pending)
 17286       || (WINDOWP (selected_window)
 17287           && (w = XWINDOW (selected_window)) != sw))
 17288     goto retry;
 17289 
 17290   /* Clear the face and image caches.
 17291 
 17292      We used to do this only if consider_all_windows_p.  But the cache
 17293      needs to be cleared if a timer creates images in the current
 17294      buffer (e.g. the test case in Bug#6230).  */
 17295 
 17296   if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
 17297     {
 17298       clear_face_cache (false);
 17299       clear_face_cache_count = 0;
 17300     }
 17301 
 17302 #ifdef HAVE_WINDOW_SYSTEM
 17303   if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
 17304     {
 17305       clear_image_caches (Qnil);
 17306       clear_image_cache_count = 0;
 17307     }
 17308 #endif /* HAVE_WINDOW_SYSTEM */
 17309 
 17310  end_of_redisplay:
 17311 #ifdef HAVE_NS
 17312   ns_set_doc_edited ();
 17313 #endif
 17314   if (interrupt_input && interrupts_deferred)
 17315     request_sigio ();
 17316 
 17317   /* We're done with this redisplay cycle, so reset the tick count in
 17318      preparation for the next redisplay cycle.  */
 17319   if (max_redisplay_ticks > 0)
 17320     update_redisplay_ticks (0, NULL);
 17321 
 17322   unbind_to (count, Qnil);
 17323   RESUME_POLLING;
 17324 }
 17325 
 17326 static void
 17327 unwind_redisplay_preserve_echo_area (void)
 17328 {
 17329   unblock_buffer_flips ();
 17330 }
 17331 
 17332 /* Redisplay, but leave alone any recent echo area message unless
 17333    another message has been requested in its place.
 17334 
 17335    This is useful in situations where you need to redisplay but no
 17336    user action has occurred, making it inappropriate for the message
 17337    area to be cleared.  See tracking_off and
 17338    wait_reading_process_output for examples of these situations.
 17339 
 17340    FROM_WHERE is an integer saying from where this function was
 17341    called.  This is useful for debugging.  */
 17342 
 17343 void
 17344 redisplay_preserve_echo_area (int from_where)
 17345 {
 17346   redisplay_trace ("redisplay_preserve_echo_area (%d)\n", from_where);
 17347 
 17348   block_input ();
 17349   specpdl_ref count = SPECPDL_INDEX ();
 17350   record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
 17351   block_buffer_flips ();
 17352   unblock_input ();
 17353 
 17354   if (!NILP (echo_area_buffer[1]))
 17355     {
 17356       /* We have a previously displayed message, but no current
 17357          message.  Redisplay the previous message.  */
 17358       display_last_displayed_message_p = true;
 17359       redisplay_internal ();
 17360       display_last_displayed_message_p = false;
 17361     }
 17362   else
 17363     redisplay_internal ();
 17364 
 17365   flush_frame (SELECTED_FRAME ());
 17366   unbind_to (count, Qnil);
 17367 }
 17368 
 17369 
 17370 /* Function registered with record_unwind_protect in redisplay_internal.  */
 17371 
 17372 static void
 17373 unwind_redisplay (void)
 17374 {
 17375   redisplaying_p = false;
 17376   unblock_buffer_flips ();
 17377 }
 17378 
 17379 /* Function registered with record_unwind_protect before calling
 17380    start_display outside of redisplay_internal.  */
 17381 void
 17382 unwind_display_working_on_window (void)
 17383 {
 17384   display_working_on_window_p = false;
 17385 }
 17386 
 17387 /* Mark the display of leaf window W as accurate or inaccurate.
 17388    If ACCURATE_P, mark display of W as accurate.
 17389    If !ACCURATE_P, arrange for W to be redisplayed the next
 17390    time redisplay_internal is called.  */
 17391 
 17392 static void
 17393 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
 17394 {
 17395   struct buffer *b = XBUFFER (w->contents);
 17396 
 17397   w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
 17398   w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
 17399   w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
 17400 
 17401   if (accurate_p)
 17402     {
 17403       b->clip_changed = false;
 17404       b->prevent_redisplay_optimizations_p = false;
 17405       eassert (buffer_window_count (b) > 0);
 17406       /* Resetting b->text->redisplay is problematic!
 17407          In order to make it safer to do it here, redisplay_internal must
 17408          have copied all b->text->redisplay to their respective windows.  */
 17409       b->text->redisplay = false;
 17410 
 17411       BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
 17412       BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
 17413       BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
 17414       BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 17415 
 17416       w->current_matrix->buffer = b;
 17417       w->current_matrix->begv = BUF_BEGV (b);
 17418       w->current_matrix->zv = BUF_ZV (b);
 17419       w->current_matrix->header_line_p = window_wants_header_line (w);
 17420       w->current_matrix->tab_line_p = window_wants_tab_line (w);
 17421 
 17422       w->last_cursor_vpos = w->cursor.vpos;
 17423       w->last_cursor_off_p = w->cursor_off_p;
 17424 
 17425       if (w == XWINDOW (selected_window))
 17426         w->last_point = BUF_PT (b);
 17427       else
 17428         w->last_point = marker_position (w->pointm);
 17429 
 17430       w->window_end_valid = true;
 17431       w->update_mode_line = false;
 17432       w->preserve_vscroll_p = false;
 17433     }
 17434 
 17435   w->redisplay = !accurate_p;
 17436 }
 17437 
 17438 
 17439 /* Mark the display of windows in the window tree rooted at WINDOW as
 17440    accurate or inaccurate.  If ACCURATE_P, mark display of
 17441    windows as accurate.  If !ACCURATE_P, arrange for windows to
 17442    be redisplayed the next time redisplay_internal is called.  */
 17443 
 17444 void
 17445 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
 17446 {
 17447   struct window *w;
 17448 
 17449   for (; !NILP (window); window = w->next)
 17450     {
 17451       w = XWINDOW (window);
 17452       if (WINDOWP (w->contents))
 17453         mark_window_display_accurate (w->contents, accurate_p);
 17454       else
 17455         mark_window_display_accurate_1 (w, accurate_p);
 17456     }
 17457 
 17458   if (accurate_p)
 17459     update_overlay_arrows (1);
 17460   else
 17461     /* Force a thorough redisplay the next time by setting
 17462        last_arrow_position and last_arrow_string to t, which is
 17463        unequal to any useful value of Voverlay_arrow_...  */
 17464     update_overlay_arrows (-1);
 17465 }
 17466 
 17467 
 17468 /* Return value in display table DP (Lisp_Char_Table *) for character
 17469    C.  Since a display table doesn't have any parent, we don't have to
 17470    follow parent.  Do not call this function directly but use the
 17471    macro DISP_CHAR_VECTOR.  */
 17472 
 17473 Lisp_Object
 17474 disp_char_vector (struct Lisp_Char_Table *dp, int c)
 17475 {
 17476   Lisp_Object val;
 17477 
 17478   if (ASCII_CHAR_P (c))
 17479     {
 17480       val = dp->ascii;
 17481       if (SUB_CHAR_TABLE_P (val))
 17482         val = XSUB_CHAR_TABLE (val)->contents[c];
 17483     }
 17484   else
 17485     {
 17486       Lisp_Object table;
 17487 
 17488       XSETCHAR_TABLE (table, dp);
 17489       val = char_table_ref (table, c);
 17490     }
 17491   if (NILP (val))
 17492     val = dp->defalt;
 17493   return val;
 17494 }
 17495 
 17496 static int buffer_flip_blocked_depth;
 17497 
 17498 static void
 17499 block_buffer_flips (void)
 17500 {
 17501   eassert (buffer_flip_blocked_depth >= 0);
 17502   buffer_flip_blocked_depth++;
 17503 }
 17504 
 17505 static void
 17506 unblock_buffer_flips (void)
 17507 {
 17508   eassert (buffer_flip_blocked_depth > 0);
 17509   if (--buffer_flip_blocked_depth == 0)
 17510     {
 17511       Lisp_Object tail, frame;
 17512       block_input ();
 17513       FOR_EACH_FRAME (tail, frame)
 17514         {
 17515           struct frame *f = XFRAME (frame);
 17516           if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
 17517             (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
 17518         }
 17519       unblock_input ();
 17520     }
 17521 }
 17522 
 17523 bool
 17524 buffer_flipping_blocked_p (void)
 17525 {
 17526   return buffer_flip_blocked_depth > 0;
 17527 }
 17528 
 17529 
 17530 /***********************************************************************
 17531                            Window Redisplay
 17532  ***********************************************************************/
 17533 
 17534 /* Redisplay all leaf windows in the window tree rooted at WINDOW.  */
 17535 
 17536 static void
 17537 redisplay_windows (Lisp_Object window)
 17538 {
 17539   while (!NILP (window))
 17540     {
 17541       struct window *w = XWINDOW (window);
 17542 
 17543       if (WINDOWP (w->contents))
 17544         redisplay_windows (w->contents);
 17545       else if (BUFFERP (w->contents))
 17546         {
 17547           displayed_buffer = XBUFFER (w->contents);
 17548           /* Use list_of_error, not Qerror, so that
 17549              we catch only errors and don't run the debugger.  */
 17550           internal_condition_case_1 (redisplay_window_0, window,
 17551                                      list_of_error,
 17552                                      redisplay_window_error);
 17553         }
 17554 
 17555       window = w->next;
 17556     }
 17557 }
 17558 
 17559 static Lisp_Object
 17560 redisplay_window_error (Lisp_Object error_data)
 17561 {
 17562   displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
 17563 
 17564   /* When in redisplay, the error is captured and not shown.  Arrange
 17565      for it to be shown later.  */
 17566   if (max_redisplay_ticks > 0
 17567       && CONSP (error_data)
 17568       && EQ (XCAR (error_data), Qerror)
 17569       && STRINGP (XCAR (XCDR (error_data))))
 17570     Vdelayed_warnings_list = Fcons (list2 (XCAR (error_data),
 17571                                            XCAR (XCDR (error_data))),
 17572                                     Vdelayed_warnings_list);
 17573   return Qnil;
 17574 }
 17575 
 17576 static Lisp_Object
 17577 redisplay_window_0 (Lisp_Object window)
 17578 {
 17579   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17580     redisplay_window (window, false);
 17581   return Qnil;
 17582 }
 17583 
 17584 static Lisp_Object
 17585 redisplay_window_1 (Lisp_Object window)
 17586 {
 17587   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17588     redisplay_window (window, true);
 17589   return Qnil;
 17590 }
 17591 
 17592 
 17593 /***********************************************************************
 17594                       Aborting runaway redisplay
 17595  ***********************************************************************/
 17596 
 17597 /* Update the redisplay-tick count for window W, and signal an error
 17598    if the tick count is above some threshold, indicating that
 17599    redisplay of the window takes "too long".
 17600 
 17601    TICKS is the amount of ticks to add to the W's current count; zero
 17602    means to initialize the tick count to zero.
 17603 
 17604    W can be NULL if TICKS is zero: that means unconditionally
 17605    re-initialize the current tick count to zero.
 17606 
 17607    W can also be NULL if the caller doesn't know which window is being
 17608    processed by the display code.  In that case, if TICKS is non-zero,
 17609    we assume it's the last window that shows the current buffer.  */
 17610 void
 17611 update_redisplay_ticks (int ticks, struct window *w)
 17612 {
 17613   /* This keeps track of the window on which redisplay is working.  */
 17614   static struct window *cwindow;
 17615   static EMACS_INT window_ticks;
 17616 
 17617   /* We only initialize the count if this is a different window or
 17618      NULL.  Otherwise, this is a call from init_iterator for the same
 17619      window we tracked before, and we should keep the count.  */
 17620   if (!ticks && w != cwindow)
 17621     {
 17622       cwindow = w;
 17623       window_ticks = 0;
 17624     }
 17625   /* Some callers can be run in contexts unrelated to display code, so
 17626      don't abort them and don't update the tick count in those cases.  */
 17627   if ((!w && !redisplaying_p && !display_working_on_window_p)
 17628       /* We never disable redisplay of a mini-window, since that is
 17629          absolutely essential for communicating with Emacs.  */
 17630       || (w && MINI_WINDOW_P (w)))
 17631     return;
 17632 
 17633   if (ticks > 0)
 17634     window_ticks += ticks;
 17635   if (max_redisplay_ticks > 0 && window_ticks > max_redisplay_ticks)
 17636     {
 17637       /* In addition to a buffer, this could be a window (for non-leaf
 17638          windows, not expected here) or nil (for pseudo-windows like
 17639          the one used for the native tool bar).  */
 17640       Lisp_Object contents = w ? w->contents : Qnil;
 17641       char *bufname =
 17642         NILP (contents)
 17643         ? SSDATA (BVAR (current_buffer, name))
 17644         : (BUFFERP (contents)
 17645            ? SSDATA (BVAR (XBUFFER (contents), name))
 17646            : (char *) "<unknown>");
 17647 
 17648       windows_or_buffers_changed = 177;
 17649       /* scrolling_window depends too much on the glyph matrices being
 17650          correct, and we cannot guarantee that if we abort the
 17651          redisplay of this window.  */
 17652       if (w && w->desired_matrix)
 17653         w->desired_matrix->no_scrolling_p = true;
 17654       error ("Window showing buffer %s takes too long to redisplay", bufname);
 17655     }
 17656 }
 17657 
 17658 
 17659 
 17660 /* Set cursor position of W.  PT is assumed to be displayed in ROW.
 17661    DELTA and DELTA_BYTES are the numbers of characters and bytes by
 17662    which positions recorded in ROW differ from current buffer
 17663    positions.
 17664 
 17665    Return true iff cursor is on this row.  */
 17666 
 17667 static bool
 17668 set_cursor_from_row (struct window *w, struct glyph_row *row,
 17669                      struct glyph_matrix *matrix,
 17670                      ptrdiff_t delta, ptrdiff_t delta_bytes,
 17671                      int dy, int dvpos)
 17672 {
 17673   struct glyph *glyph = row->glyphs[TEXT_AREA];
 17674   struct glyph *end = glyph + row->used[TEXT_AREA];
 17675   struct glyph *cursor = NULL;
 17676   /* The last known character position in row.  */
 17677   ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
 17678   int x = row->x;
 17679   ptrdiff_t pt_old = PT - delta;
 17680   ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 17681   ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 17682   struct glyph *glyph_before = glyph - 1, *glyph_after = end;
 17683   /* A glyph beyond the edge of TEXT_AREA which we should never
 17684      touch.  */
 17685   struct glyph *glyphs_end = end;
 17686   /* True means we've found a match for cursor position, but that
 17687      glyph has the avoid_cursor_p flag set.  */
 17688   bool match_with_avoid_cursor = false;
 17689   /* True means we've seen at least one glyph that came from a
 17690      display string.  */
 17691   bool string_seen = false;
 17692   /* Largest and smallest buffer positions seen so far during scan of
 17693      glyph row.  */
 17694   ptrdiff_t bpos_max = pos_before;
 17695   ptrdiff_t bpos_min = pos_after;
 17696   /* Last buffer position covered by an overlay string with an integer
 17697      `cursor' property.  */
 17698   ptrdiff_t bpos_covered = 0;
 17699   /* True means the display string on which to display the cursor
 17700      comes from a text property, not from an overlay.  */
 17701   bool string_from_text_prop = false;
 17702 
 17703   /* Don't even try doing anything if called for a mode-line or
 17704      header-line or tab-line row, since the rest of the code isn't
 17705      prepared to deal with such calamities.  */
 17706   eassert (!row->mode_line_p);
 17707   if (row->mode_line_p)
 17708     return false;
 17709 
 17710   /* Skip over glyphs not having an object at the start and the end of
 17711      the row.  These are special glyphs like truncation marks on
 17712      terminal frames.  */
 17713   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 17714     {
 17715       if (!row->reversed_p)
 17716         {
 17717           while (glyph < end
 17718                  && NILP (glyph->object)
 17719                  && glyph->charpos < 0)
 17720             {
 17721               x += glyph->pixel_width;
 17722               ++glyph;
 17723             }
 17724           while (end > glyph
 17725                  && NILP ((end - 1)->object)
 17726                  /* CHARPOS is zero for blanks and stretch glyphs
 17727                     inserted by extend_face_to_end_of_line.  */
 17728                  && (end - 1)->charpos <= 0)
 17729             --end;
 17730           glyph_before = glyph - 1;
 17731           glyph_after = end;
 17732         }
 17733       else
 17734         {
 17735           struct glyph *g;
 17736 
 17737           /* If the glyph row is reversed, we need to process it from back
 17738              to front, so swap the edge pointers.  */
 17739           glyphs_end = end = glyph - 1;
 17740           glyph += row->used[TEXT_AREA] - 1;
 17741 
 17742           while (glyph > end + 1
 17743                  && NILP (glyph->object)
 17744                  && glyph->charpos < 0)
 17745             --glyph;
 17746           if (NILP (glyph->object) && glyph->charpos < 0)
 17747             --glyph;
 17748           /* By default, in reversed rows we put the cursor on the
 17749              rightmost (first in the reading order) glyph.  */
 17750           for (x = 0, g = end + 1; g < glyph; g++)
 17751             x += g->pixel_width;
 17752           while (end < glyph
 17753                  && NILP ((end + 1)->object)
 17754                  && (end + 1)->charpos <= 0)
 17755             ++end;
 17756           glyph_before = glyph + 1;
 17757           glyph_after = end;
 17758         }
 17759     }
 17760   else if (row->reversed_p)
 17761     {
 17762       /* In R2L rows that don't display text, put the cursor on the
 17763          rightmost glyph.  Case in point: an empty last line that is
 17764          part of an R2L paragraph.  */
 17765       cursor = end - 1;
 17766       /* Avoid placing the cursor on the last glyph of the row, where
 17767          on terminal frames we hold the vertical border between
 17768          adjacent windows.  */
 17769       if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
 17770           && !WINDOW_RIGHTMOST_P (w)
 17771           && cursor == row->glyphs[LAST_AREA] - 1)
 17772         cursor--;
 17773       x = -1;   /* will be computed below, at label compute_x */
 17774     }
 17775 
 17776   /* Step 1: Try to find the glyph whose character position
 17777      corresponds to point.  If that's not possible, find 2 glyphs
 17778      whose character positions are the closest to point, one before
 17779      point, the other after it.  */
 17780   if (!row->reversed_p)
 17781     while (/* not marched to end of glyph row */
 17782            glyph < end
 17783            /* glyph was not inserted by redisplay for internal purposes */
 17784            && !NILP (glyph->object))
 17785       {
 17786         if (BUFFERP (glyph->object))
 17787           {
 17788             ptrdiff_t dpos = glyph->charpos - pt_old;
 17789 
 17790             if (glyph->charpos > bpos_max)
 17791               bpos_max = glyph->charpos;
 17792             if (glyph->charpos < bpos_min)
 17793               bpos_min = glyph->charpos;
 17794             if (!glyph->avoid_cursor_p)
 17795               {
 17796                 /* If we hit point, we've found the glyph on which to
 17797                    display the cursor.  */
 17798                 if (dpos == 0)
 17799                   {
 17800                     match_with_avoid_cursor = false;
 17801                     break;
 17802                   }
 17803                 /* See if we've found a better approximation to
 17804                    POS_BEFORE or to POS_AFTER.  */
 17805                 if (0 > dpos && dpos > pos_before - pt_old)
 17806                   {
 17807                     pos_before = glyph->charpos;
 17808                     glyph_before = glyph;
 17809                   }
 17810                 else if (0 < dpos && dpos < pos_after - pt_old)
 17811                   {
 17812                     pos_after = glyph->charpos;
 17813                     glyph_after = glyph;
 17814                   }
 17815               }
 17816             else if (dpos == 0)
 17817               match_with_avoid_cursor = true;
 17818           }
 17819         else if (STRINGP (glyph->object))
 17820           {
 17821             Lisp_Object chprop;
 17822             ptrdiff_t glyph_pos = glyph->charpos;
 17823 
 17824             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 17825                                          glyph->object);
 17826             if (!NILP (chprop))
 17827               {
 17828                 /* If the string came from a `display' text property,
 17829                    look up the buffer position of that property and
 17830                    use that position to update bpos_max, as if we
 17831                    actually saw such a position in one of the row's
 17832                    glyphs.  This helps with supporting integer values
 17833                    of `cursor' property on the display string in
 17834                    situations where most or all of the row's buffer
 17835                    text is completely covered by display properties,
 17836                    so that no glyph with valid buffer positions is
 17837                    ever seen in the row.  */
 17838                 ptrdiff_t prop_pos =
 17839                   string_buffer_position_lim (glyph->object, pos_before,
 17840                                               pos_after, false);
 17841 
 17842                 if (prop_pos >= pos_before)
 17843                   bpos_max = prop_pos;
 17844               }
 17845             if (FIXNUMP (chprop))
 17846               {
 17847                 bpos_covered = bpos_max + XFIXNUM (chprop);
 17848                 /* If the `cursor' property covers buffer positions up
 17849                    to and including point, we should display cursor on
 17850                    this glyph.  Note that, if a `cursor' property on one
 17851                    of the string's characters has an integer value, we
 17852                    will break out of the loop below _before_ we get to
 17853                    the position match above.  IOW, integer values of
 17854                    the `cursor' property override the "exact match for
 17855                    point" strategy of positioning the cursor.  */
 17856                 /* Implementation note: bpos_max == pt_old when, e.g.,
 17857                    we are in an empty line, where bpos_max is set to
 17858                    MATRIX_ROW_START_CHARPOS, see above.  */
 17859                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 17860                   {
 17861                     cursor = glyph;
 17862                     break;
 17863                   }
 17864               }
 17865 
 17866             string_seen = true;
 17867           }
 17868         x += glyph->pixel_width;
 17869         ++glyph;
 17870       }
 17871   else if (glyph > end) /* row is reversed */
 17872     while (!NILP (glyph->object))
 17873       {
 17874         if (BUFFERP (glyph->object))
 17875           {
 17876             ptrdiff_t dpos = glyph->charpos - pt_old;
 17877 
 17878             if (glyph->charpos > bpos_max)
 17879               bpos_max = glyph->charpos;
 17880             if (glyph->charpos < bpos_min)
 17881               bpos_min = glyph->charpos;
 17882             if (!glyph->avoid_cursor_p)
 17883               {
 17884                 if (dpos == 0)
 17885                   {
 17886                     match_with_avoid_cursor = false;
 17887                     break;
 17888                   }
 17889                 if (0 > dpos && dpos > pos_before - pt_old)
 17890                   {
 17891                     pos_before = glyph->charpos;
 17892                     glyph_before = glyph;
 17893                   }
 17894                 else if (0 < dpos && dpos < pos_after - pt_old)
 17895                   {
 17896                     pos_after = glyph->charpos;
 17897                     glyph_after = glyph;
 17898                   }
 17899               }
 17900             else if (dpos == 0)
 17901               match_with_avoid_cursor = true;
 17902           }
 17903         else if (STRINGP (glyph->object))
 17904           {
 17905             Lisp_Object chprop;
 17906             ptrdiff_t glyph_pos = glyph->charpos;
 17907 
 17908             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 17909                                          glyph->object);
 17910             if (!NILP (chprop))
 17911               {
 17912                 ptrdiff_t prop_pos =
 17913                   string_buffer_position_lim (glyph->object, pos_before,
 17914                                               pos_after, false);
 17915 
 17916                 if (prop_pos >= pos_before)
 17917                   bpos_max = prop_pos;
 17918               }
 17919             if (FIXNUMP (chprop))
 17920               {
 17921                 bpos_covered = bpos_max + XFIXNUM (chprop);
 17922                 /* If the `cursor' property covers buffer positions up
 17923                    to and including point, we should display cursor on
 17924                    this glyph.  */
 17925                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 17926                   {
 17927                     cursor = glyph;
 17928                     break;
 17929                   }
 17930               }
 17931             string_seen = true;
 17932           }
 17933         --glyph;
 17934         if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
 17935           {
 17936             x--;                /* can't use any pixel_width */
 17937             break;
 17938           }
 17939         x -= glyph->pixel_width;
 17940     }
 17941 
 17942   /* Step 2: If we didn't find an exact match for point, we need to
 17943      look for a proper place to put the cursor among glyphs between
 17944      GLYPH_BEFORE and GLYPH_AFTER.  */
 17945   if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 17946         && BUFFERP (glyph->object) && glyph->charpos == pt_old)
 17947       && !(bpos_max <= pt_old && pt_old <= bpos_covered))
 17948     {
 17949       /* An empty line has a single glyph whose OBJECT is nil and
 17950          whose CHARPOS is the position of a newline on that line.
 17951          Note that on a TTY, there are more glyphs after that, which
 17952          were produced by extend_face_to_end_of_line, but their
 17953          CHARPOS is zero or negative.  */
 17954       bool empty_line_p =
 17955         ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 17956          && NILP (glyph->object) && glyph->charpos > 0
 17957          /* On a TTY, continued and truncated rows also have a glyph at
 17958             their end whose OBJECT is nil and whose CHARPOS is
 17959             positive (the continuation and truncation glyphs), but such
 17960             rows are obviously not "empty".  */
 17961          && !(row->continued_p || row->truncated_on_right_p));
 17962 
 17963       if (row->ends_in_ellipsis_p && pos_after == last_pos)
 17964         {
 17965           ptrdiff_t ellipsis_pos;
 17966 
 17967           /* Scan back over the ellipsis glyphs.  */
 17968           if (!row->reversed_p)
 17969             {
 17970               ellipsis_pos = (glyph - 1)->charpos;
 17971               while (glyph > row->glyphs[TEXT_AREA]
 17972                      && (glyph - 1)->charpos == ellipsis_pos)
 17973                 glyph--, x -= glyph->pixel_width;
 17974               /* That loop always goes one position too far, including
 17975                  the glyph before the ellipsis.  So scan forward over
 17976                  that one.  */
 17977               x += glyph->pixel_width;
 17978               glyph++;
 17979             }
 17980           else  /* row is reversed */
 17981             {
 17982               ellipsis_pos = (glyph + 1)->charpos;
 17983               while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 17984                      && (glyph + 1)->charpos == ellipsis_pos)
 17985                 glyph++, x += glyph->pixel_width;
 17986               x -= glyph->pixel_width;
 17987               glyph--;
 17988             }
 17989         }
 17990       else if (match_with_avoid_cursor)
 17991         {
 17992           cursor = glyph_after;
 17993           x = -1;
 17994         }
 17995       else if (string_seen)
 17996         {
 17997           int incr = row->reversed_p ? -1 : +1;
 17998 
 17999           /* Need to find the glyph that came out of a string which is
 18000              present at point.  That glyph is somewhere between
 18001              GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
 18002              positioned between POS_BEFORE and POS_AFTER in the
 18003              buffer.  */
 18004           struct glyph *start, *stop;
 18005           ptrdiff_t pos = pos_before;
 18006 
 18007           x = -1;
 18008 
 18009           /* If the row ends in a newline from a display string,
 18010              reordering could have moved the glyphs belonging to the
 18011              string out of the [GLYPH_BEFORE..GLYPH_AFTER] range.  So
 18012              in this case we extend the search to the last glyph in
 18013              the row that was not inserted by redisplay.  */
 18014           if (row->ends_in_newline_from_string_p)
 18015             {
 18016               glyph_after = end;
 18017               pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 18018             }
 18019 
 18020           /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
 18021              correspond to POS_BEFORE and POS_AFTER, respectively.  We
 18022              need START and STOP in the order that corresponds to the
 18023              row's direction as given by its reversed_p flag.  If the
 18024              directionality of characters between POS_BEFORE and
 18025              POS_AFTER is the opposite of the row's base direction,
 18026              these characters will have been reordered for display,
 18027              and we need to reverse START and STOP.  */
 18028           if (!row->reversed_p)
 18029             {
 18030               start = min (glyph_before, glyph_after);
 18031               stop = max (glyph_before, glyph_after);
 18032             }
 18033           else
 18034             {
 18035               start = max (glyph_before, glyph_after);
 18036               stop = min (glyph_before, glyph_after);
 18037             }
 18038           for (glyph = start + incr;
 18039                row->reversed_p ? glyph > stop : glyph < stop; )
 18040             {
 18041 
 18042               /* Any glyphs that come from the buffer are here because
 18043                  of bidi reordering.  Skip them, and only pay
 18044                  attention to glyphs that came from some string.  */
 18045               if (STRINGP (glyph->object))
 18046                 {
 18047                   Lisp_Object str;
 18048                   ptrdiff_t tem;
 18049                   /* If the display property covers the newline, we
 18050                      need to search for it one position farther.  */
 18051                   ptrdiff_t lim = pos_after
 18052                     + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
 18053 
 18054                   string_from_text_prop = false;
 18055                   str = glyph->object;
 18056                   tem = string_buffer_position_lim (str, pos, lim, false);
 18057                   if (tem == 0  /* from overlay */
 18058                       || pos <= tem)
 18059                     {
 18060                       /* If the string from which this glyph came is
 18061                          found in the buffer at point, or at position
 18062                          that is closer to point than pos_after, then
 18063                          we've found the glyph we've been looking for.
 18064                          If it comes from an overlay (tem == 0), and
 18065                          it has the `cursor' property on one of its
 18066                          glyphs, record that glyph as a candidate for
 18067                          displaying the cursor.  (As in the
 18068                          unidirectional version, we will display the
 18069                          cursor on the last candidate we find.)  */
 18070                       if (tem == 0
 18071                           || tem == pt_old
 18072                           || (tem - pt_old > 0 && tem < pos_after))
 18073                         {
 18074                           /* The glyphs from this string could have
 18075                              been reordered.  Find the one with the
 18076                              smallest string position.  Or there could
 18077                              be a character in the string with the
 18078                              `cursor' property, which means display
 18079                              cursor on that character's glyph.  */
 18080                           ptrdiff_t strpos = glyph->charpos;
 18081 
 18082                           if (tem)
 18083                             {
 18084                               cursor = glyph;
 18085                               string_from_text_prop = true;
 18086                             }
 18087                           for ( ;
 18088                                (row->reversed_p ? glyph > stop : glyph < stop)
 18089                                  && EQ (glyph->object, str);
 18090                                glyph += incr)
 18091                             {
 18092                               Lisp_Object cprop;
 18093                               ptrdiff_t gpos = glyph->charpos;
 18094 
 18095                               cprop = Fget_char_property (make_fixnum (gpos),
 18096                                                           Qcursor,
 18097                                                           glyph->object);
 18098                               if (!NILP (cprop))
 18099                                 {
 18100                                   cursor = glyph;
 18101                                   break;
 18102                                 }
 18103                               if (tem && glyph->charpos < strpos)
 18104                                 {
 18105                                   strpos = glyph->charpos;
 18106                                   cursor = glyph;
 18107                                 }
 18108                             }
 18109 
 18110                           if (tem == pt_old
 18111                               || (tem - pt_old > 0 && tem < pos_after))
 18112                             goto compute_x;
 18113                         }
 18114                       if (tem)
 18115                         pos = tem + 1; /* don't find previous instances */
 18116                     }
 18117                   /* This string is not what we want; skip all of the
 18118                      glyphs that came from it.  */
 18119                   while ((row->reversed_p ? glyph > stop : glyph < stop)
 18120                          && EQ (glyph->object, str))
 18121                     glyph += incr;
 18122                 }
 18123               else
 18124                 glyph += incr;
 18125             }
 18126 
 18127           /* If we reached the end of the line, and END was from a string,
 18128              the cursor is not on this line.  */
 18129           if (cursor == NULL
 18130               && (row->reversed_p ? glyph <= end : glyph >= end)
 18131               && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
 18132               && STRINGP (end->object)
 18133               && row->continued_p)
 18134             return false;
 18135         }
 18136       /* A truncated row may not include PT among its character positions.
 18137          Setting the cursor inside the scroll margin will trigger
 18138          recalculation of hscroll in hscroll_window_tree.  But if a
 18139          display string covers point, defer to the string-handling
 18140          code below to figure this out.  */
 18141       else if (row->truncated_on_left_p && pt_old < bpos_min)
 18142         {
 18143           cursor = glyph_before;
 18144           x = -1;
 18145         }
 18146       else if ((row->truncated_on_right_p && pt_old > bpos_max)
 18147                /* Zero-width characters produce no glyphs.  */
 18148                || (!empty_line_p
 18149                    && (row->reversed_p
 18150                        ? glyph_after > glyphs_end
 18151                        : glyph_after < glyphs_end)))
 18152         {
 18153           cursor = glyph_after;
 18154           x = -1;
 18155         }
 18156     }
 18157 
 18158  compute_x:
 18159   if (cursor != NULL)
 18160     glyph = cursor;
 18161   else if (glyph == glyphs_end
 18162            && pos_before == pos_after
 18163            && STRINGP ((row->reversed_p
 18164                         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18165                         : row->glyphs[TEXT_AREA])->object))
 18166     {
 18167       /* If all the glyphs of this row came from strings, put the
 18168          cursor on the first glyph of the row.  This avoids having the
 18169          cursor outside of the text area in this very rare and hard
 18170          use case.  */
 18171       glyph =
 18172         row->reversed_p
 18173         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18174         : row->glyphs[TEXT_AREA];
 18175     }
 18176   if (x < 0)
 18177     {
 18178       struct glyph *g;
 18179 
 18180       /* Need to compute x that corresponds to GLYPH.  */
 18181       for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
 18182         {
 18183           if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
 18184             emacs_abort ();
 18185           x += g->pixel_width;
 18186         }
 18187     }
 18188 
 18189   /* ROW could be part of a continued line, which, under bidi
 18190      reordering, might have other rows whose start and end charpos
 18191      occlude point.  Only set w->cursor if we found a better
 18192      approximation to the cursor position than we have from previously
 18193      examined candidate rows belonging to the same continued line.  */
 18194   if (/* We already have a candidate row.  */
 18195       w->cursor.vpos >= 0
 18196       /* That candidate is not the row we are processing.  */
 18197       && MATRIX_ROW (matrix, w->cursor.vpos) != row
 18198       /* Make sure cursor.vpos specifies a row whose start and end
 18199          charpos occlude point, and it is valid candidate for being a
 18200          cursor-row.  This is because some callers of this function
 18201          leave cursor.vpos at the row where the cursor was displayed
 18202          during the last redisplay cycle.  */
 18203       && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
 18204       && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18205       && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
 18206     {
 18207       struct glyph *g1
 18208         = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
 18209 
 18210       /* Don't consider glyphs that are outside TEXT_AREA.  */
 18211       if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
 18212         return false;
 18213       /* Keep the candidate whose buffer position is the closest to
 18214          point or has the `cursor' property.  */
 18215       if (/* Previous candidate is a glyph in TEXT_AREA of that row.  */
 18216           w->cursor.hpos >= 0
 18217           && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
 18218           && ((BUFFERP (g1->object)
 18219                && (g1->charpos == pt_old /* An exact match always wins.  */
 18220                    || (BUFFERP (glyph->object)
 18221                        && eabs (g1->charpos - pt_old)
 18222                        < eabs (glyph->charpos - pt_old))))
 18223               /* Previous candidate is a glyph from a string that has
 18224                  a non-nil `cursor' property.  */
 18225               || (STRINGP (g1->object)
 18226                   && (!NILP (Fget_char_property (make_fixnum (g1->charpos),
 18227                                                 Qcursor, g1->object))
 18228                       /* Previous candidate is from the same display
 18229                          string as this one, and the display string
 18230                          came from a text property.  */
 18231                       || (EQ (g1->object, glyph->object)
 18232                           && string_from_text_prop)
 18233                       /* this candidate is from newline and its
 18234                          position is not an exact match */
 18235                       || (NILP (glyph->object)
 18236                           && glyph->charpos != pt_old)))))
 18237         return false;
 18238       /* If this candidate gives an exact match, use that.  */
 18239       if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18240             /* If this candidate is a glyph created for the
 18241                terminating newline of a line, and point is on that
 18242                newline, it wins because it's an exact match.  */
 18243             || (!row->continued_p
 18244                 && NILP (glyph->object)
 18245                 && glyph->charpos == 0
 18246                 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
 18247           /* Otherwise, keep the candidate that comes from a row
 18248              spanning less buffer positions.  This may win when one or
 18249              both candidate positions are on glyphs that came from
 18250              display strings, for which we cannot compare buffer
 18251              positions.  */
 18252           && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18253              - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18254              < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
 18255         return false;
 18256     }
 18257   w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
 18258   w->cursor.x = x;
 18259   w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
 18260   w->cursor.y = row->y + dy;
 18261 
 18262   if (w == XWINDOW (selected_window))
 18263     {
 18264       if (!row->continued_p
 18265           && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 18266           && row->x == 0)
 18267         {
 18268           this_line_buffer = XBUFFER (w->contents);
 18269 
 18270           CHARPOS (this_line_start_pos)
 18271             = MATRIX_ROW_START_CHARPOS (row) + delta;
 18272           BYTEPOS (this_line_start_pos)
 18273             = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
 18274 
 18275           CHARPOS (this_line_end_pos)
 18276             = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
 18277           BYTEPOS (this_line_end_pos)
 18278             = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
 18279 
 18280           this_line_y = w->cursor.y;
 18281           this_line_pixel_height = row->height;
 18282           this_line_vpos = w->cursor.vpos;
 18283           this_line_start_x = row->x;
 18284         }
 18285       else
 18286         CHARPOS (this_line_start_pos) = 0;
 18287     }
 18288 
 18289   return true;
 18290 }
 18291 
 18292 
 18293 /* Run window scroll functions, if any, for WINDOW with new window
 18294    start STARTP.  Sets the window start of WINDOW to that position.
 18295 
 18296    We assume that the window's buffer is really current.  */
 18297 
 18298 static struct text_pos
 18299 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
 18300 {
 18301   struct window *w = XWINDOW (window);
 18302   SET_MARKER_FROM_TEXT_POS (w->start, startp);
 18303 
 18304   eassert (current_buffer == XBUFFER (w->contents));
 18305 
 18306   if (!NILP (Vwindow_scroll_functions))
 18307     {
 18308       specpdl_ref count = SPECPDL_INDEX ();
 18309       specbind (Qinhibit_quit, Qt);
 18310       safe_run_hooks_2
 18311         (Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
 18312       unbind_to (count, Qnil);
 18313       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18314       /* In case the hook functions switch buffers.  */
 18315       set_buffer_internal (XBUFFER (w->contents));
 18316     }
 18317 
 18318   return startp;
 18319 }
 18320 
 18321 
 18322 /* Make sure the line containing the cursor is fully visible.
 18323    A value of true means there is nothing to be done.
 18324    (Either the line is fully visible, or it cannot be made so,
 18325    or we cannot tell.)
 18326 
 18327    If FORCE_P, return false even if partial visible cursor row
 18328    is higher than window.
 18329 
 18330    If CURRENT_MATRIX_P, use the information from the
 18331    window's current glyph matrix; otherwise use the desired glyph
 18332    matrix.
 18333 
 18334    If JUST_TEST_USER_PREFERENCE_P, just test what the value of
 18335    make-cursor-row-fully-visible requires, don't test the actual
 18336    cursor position.  The assumption is that in that case the caller
 18337    performs the necessary testing of the cursor position.
 18338 
 18339    A value of false means the caller should do scrolling
 18340    as if point had gone off the screen.  */
 18341 
 18342 static bool
 18343 cursor_row_fully_visible_p (struct window *w, bool force_p,
 18344                             bool current_matrix_p,
 18345                             bool just_test_user_preference_p)
 18346 {
 18347   struct glyph_matrix *matrix;
 18348   struct glyph_row *row;
 18349   int window_height;
 18350   Lisp_Object mclfv_p =
 18351     buffer_local_value (Qmake_cursor_line_fully_visible, w->contents);
 18352 
 18353   /* If no local binding, use the global value.  */
 18354   if (BASE_EQ (mclfv_p, Qunbound))
 18355     mclfv_p = Vmake_cursor_line_fully_visible;
 18356   /* Follow mode sets the variable to a Lisp function in buffers that
 18357      are under Follow mode.  */
 18358   if (FUNCTIONP (mclfv_p))
 18359     {
 18360       Lisp_Object window;
 18361       XSETWINDOW (window, w);
 18362       /* Implementation note: if the function we call here signals an
 18363          error, we will NOT scroll when the cursor is partially-visible.  */
 18364       Lisp_Object val = safe_call1 (mclfv_p, window);
 18365       if (NILP (val))
 18366         return true;
 18367       else if (just_test_user_preference_p)
 18368         return false;
 18369     }
 18370   else if (NILP (mclfv_p))
 18371     return true;
 18372   else if (just_test_user_preference_p)
 18373     return false;
 18374 
 18375   /* It's not always possible to find the cursor, e.g, when a window
 18376      is full of overlay strings.  Don't do anything in that case.  */
 18377   if (w->cursor.vpos < 0)
 18378     return true;
 18379 
 18380   matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
 18381   row = MATRIX_ROW (matrix, w->cursor.vpos);
 18382 
 18383   /* If the cursor row is not partially visible, there's nothing to do.  */
 18384   if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
 18385     return true;
 18386 
 18387   /* If the row the cursor is in is taller than the window's height,
 18388      it's not clear what to do, so do nothing.  */
 18389   window_height = window_box_height (w);
 18390   if (row->height >= window_height)
 18391     {
 18392       if (!force_p || MINI_WINDOW_P (w)
 18393           || w->vscroll || w->cursor.vpos == 0)
 18394         return true;
 18395     }
 18396   return false;
 18397 }
 18398 
 18399 
 18400 /* Try scrolling PT into view in window WINDOW.  JUST_THIS_ONE_P
 18401    means only WINDOW is redisplayed in redisplay_internal.
 18402    TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
 18403    in redisplay_window to bring a partially visible line into view in
 18404    the case that only the cursor has moved.
 18405 
 18406    LAST_LINE_MISFIT should be true if we're scrolling because the
 18407    last screen line's vertical height extends past the end of the screen.
 18408 
 18409    Value is
 18410 
 18411    1    if scrolling succeeded
 18412 
 18413    0    if scrolling didn't find point.
 18414 
 18415    -1   if new fonts have been loaded so that we must interrupt
 18416    redisplay, adjust glyph matrices, and try again.  */
 18417 
 18418 enum
 18419 {
 18420   SCROLLING_SUCCESS = 1,
 18421   SCROLLING_FAILED = 0,
 18422   SCROLLING_NEED_LARGER_MATRICES = -1
 18423 };
 18424 
 18425 /* If scroll-conservatively is more than this, never recenter.
 18426 
 18427    If you change this, don't forget to update the doc string of
 18428    `scroll-conservatively' and the Emacs manual.  */
 18429 #define SCROLL_LIMIT 100
 18430 
 18431 static int
 18432 try_scrolling (Lisp_Object window, bool just_this_one_p,
 18433                intmax_t arg_scroll_conservatively, intmax_t scroll_step,
 18434                bool temp_scroll_step, bool last_line_misfit)
 18435 {
 18436   struct window *w = XWINDOW (window);
 18437   struct text_pos pos, startp;
 18438   struct it it;
 18439   int this_scroll_margin, scroll_max, rc, height;
 18440   int dy = 0, amount_to_scroll = 0;
 18441   bool scroll_down_p = false;
 18442   int extra_scroll_margin_lines = last_line_misfit;
 18443   Lisp_Object aggressive;
 18444   /* We will never try scrolling more than this number of lines.  */
 18445   int scroll_limit = SCROLL_LIMIT;
 18446   int frame_line_height = default_line_pixel_height (w);
 18447 
 18448 #ifdef GLYPH_DEBUG
 18449   debug_method_add (w, "try_scrolling");
 18450 #endif
 18451 
 18452   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18453 
 18454   this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18455 
 18456   /* Force arg_scroll_conservatively to have a reasonable value, to
 18457      avoid scrolling too far away with slow move_it_* functions.  Note
 18458      that the user can supply scroll-conservatively equal to
 18459      `most-positive-fixnum', which can be larger than INT_MAX.  */
 18460   if (arg_scroll_conservatively > scroll_limit)
 18461     {
 18462       arg_scroll_conservatively = scroll_limit + 1;
 18463       scroll_max = scroll_limit * frame_line_height;
 18464     }
 18465   else if (0 < scroll_step || 0 < arg_scroll_conservatively || temp_scroll_step)
 18466     /* Compute how much we should try to scroll maximally to bring
 18467        point into view.  */
 18468     {
 18469       intmax_t scroll_lines_max
 18470         = max (scroll_step, max (arg_scroll_conservatively, temp_scroll_step));
 18471       int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
 18472       scroll_max = scroll_lines * frame_line_height;
 18473     }
 18474   else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
 18475            || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
 18476     /* We're trying to scroll because of aggressive scrolling but no
 18477        scroll_step is set.  Choose an arbitrary one.  */
 18478     scroll_max = 10 * frame_line_height;
 18479   else
 18480     scroll_max = 0;
 18481 
 18482  too_near_end:
 18483 
 18484   /* Decide whether to scroll down.  */
 18485   if (PT > CHARPOS (startp))
 18486     {
 18487       int scroll_margin_y;
 18488 
 18489       /* Compute the pixel ypos of the scroll margin, then move IT to
 18490          either that ypos or PT, whichever comes first.  */
 18491       start_display (&it, w, startp);
 18492       scroll_margin_y = it.last_visible_y - partial_line_height (&it)
 18493         - this_scroll_margin
 18494         - frame_line_height * extra_scroll_margin_lines;
 18495       move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
 18496                   (MOVE_TO_POS | MOVE_TO_Y));
 18497 
 18498       if (PT > CHARPOS (it.current.pos))
 18499         {
 18500           int y0 = line_bottom_y (&it);
 18501           /* Compute how many pixels below window bottom to stop searching
 18502              for PT.  This avoids costly search for PT that is far away if
 18503              the user limited scrolling by a small number of lines, but
 18504              always finds PT if scroll_conservatively is set to a large
 18505              number, such as most-positive-fixnum.  */
 18506           int slack = max (scroll_max, 10 * frame_line_height);
 18507           int y_to_move = it.last_visible_y + slack;
 18508 
 18509           /* Compute the distance from the scroll margin to PT or to
 18510              the scroll limit, whichever comes first.  This should
 18511              include the height of the cursor line, to make that line
 18512              fully visible.  */
 18513           move_it_to (&it, PT, -1, y_to_move,
 18514                       -1, MOVE_TO_POS | MOVE_TO_Y);
 18515           dy = line_bottom_y (&it) - y0;
 18516 
 18517           if (dy > scroll_max)
 18518             return SCROLLING_FAILED;
 18519 
 18520           if (dy > 0)
 18521             scroll_down_p = true;
 18522         }
 18523       else if (PT == IT_CHARPOS (it)
 18524                && IT_CHARPOS (it) < ZV
 18525                && it.method == GET_FROM_STRING
 18526                && arg_scroll_conservatively > scroll_limit
 18527                && it.current_x == 0)
 18528         {
 18529           enum move_it_result skip;
 18530           int y1 = it.current_y;
 18531           int vpos;
 18532 
 18533           /* A before-string that includes newlines and is displayed
 18534              on the last visible screen line could fail us under
 18535              scroll-conservatively > 100, because we will be unable to
 18536              position the cursor on that last visible line.  Try to
 18537              recover by finding the first screen line that has some
 18538              glyphs coming from the buffer text.  */
 18539           do {
 18540             skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 18541             if (skip != MOVE_NEWLINE_OR_CR
 18542                 || IT_CHARPOS (it) != PT
 18543                 || it.method == GET_FROM_BUFFER)
 18544               break;
 18545             vpos = it.vpos;
 18546             move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
 18547           } while (it.vpos > vpos);
 18548 
 18549           dy = it.current_y - y1;
 18550 
 18551           if (dy > scroll_max)
 18552             return SCROLLING_FAILED;
 18553 
 18554           if (dy > 0)
 18555             scroll_down_p = true;
 18556         }
 18557     }
 18558 
 18559   if (scroll_down_p)
 18560     {
 18561       /* Point is in or below the bottom scroll margin, so move the
 18562          window start down.  If scrolling conservatively, move it just
 18563          enough down to make point visible.  If scroll_step is set,
 18564          move it down by scroll_step.  */
 18565       if (arg_scroll_conservatively)
 18566         amount_to_scroll
 18567           = min (max (dy, frame_line_height),
 18568                  frame_line_height * arg_scroll_conservatively);
 18569       else if (scroll_step || temp_scroll_step)
 18570         amount_to_scroll = scroll_max;
 18571       else
 18572         {
 18573           aggressive = BVAR (current_buffer, scroll_up_aggressively);
 18574           height = WINDOW_BOX_TEXT_HEIGHT (w);
 18575           if (NUMBERP (aggressive))
 18576             {
 18577               double float_amount = XFLOATINT (aggressive) * height;
 18578               int aggressive_scroll = float_amount;
 18579               if (aggressive_scroll == 0 && float_amount > 0)
 18580                 aggressive_scroll = 1;
 18581               /* Don't let point enter the scroll margin near top of
 18582                  the window.  This could happen if the value of
 18583                  scroll_up_aggressively is too large and there are
 18584                  non-zero margins, because scroll_up_aggressively
 18585                  means put point that fraction of window height
 18586                  _from_the_bottom_margin_.  */
 18587               if (aggressive_scroll + 2 * this_scroll_margin > height)
 18588                 aggressive_scroll = height - 2 * this_scroll_margin;
 18589               amount_to_scroll = dy + aggressive_scroll;
 18590             }
 18591         }
 18592 
 18593       if (amount_to_scroll <= 0)
 18594         return SCROLLING_FAILED;
 18595 
 18596       start_display (&it, w, startp);
 18597       if (arg_scroll_conservatively <= scroll_limit)
 18598         move_it_vertically (&it, amount_to_scroll);
 18599       else
 18600         {
 18601           /* Extra precision for users who set scroll-conservatively
 18602              to a large number: make sure the amount we scroll
 18603              the window start is never less than amount_to_scroll,
 18604              which was computed as distance from window bottom to
 18605              point.  This matters when lines at window top and lines
 18606              below window bottom have different height.  */
 18607           struct it it1;
 18608           void *it1data = NULL;
 18609           /* We use a temporary it1 because line_bottom_y can modify
 18610              its argument, if it moves one line down; see there.  */
 18611           int start_y;
 18612 
 18613           SAVE_IT (it1, it, it1data);
 18614           start_y = line_bottom_y (&it1);
 18615           do {
 18616             RESTORE_IT (&it, &it, it1data);
 18617             move_it_by_lines (&it, 1);
 18618             SAVE_IT (it1, it, it1data);
 18619           } while (IT_CHARPOS (it) < ZV
 18620                    && line_bottom_y (&it1) - start_y < amount_to_scroll);
 18621           bidi_unshelve_cache (it1data, true);
 18622         }
 18623 
 18624       /* If STARTP is unchanged, move it down another screen line.  */
 18625       if (IT_CHARPOS (it) == CHARPOS (startp))
 18626         move_it_by_lines (&it, 1);
 18627       startp = it.current.pos;
 18628     }
 18629   else
 18630     {
 18631       struct text_pos scroll_margin_pos = startp;
 18632       int y_offset = 0;
 18633 
 18634       /* See if point is inside the scroll margin at the top of the
 18635          window.  */
 18636       if (this_scroll_margin)
 18637         {
 18638           int y_start;
 18639 
 18640           start_display (&it, w, startp);
 18641           y_start = it.current_y;
 18642           move_it_vertically (&it, this_scroll_margin);
 18643           scroll_margin_pos = it.current.pos;
 18644           /* If we didn't move enough before hitting ZV, request
 18645              additional amount of scroll, to move point out of the
 18646              scroll margin.  */
 18647           if (IT_CHARPOS (it) == ZV
 18648               && it.current_y - y_start < this_scroll_margin)
 18649             y_offset = this_scroll_margin - (it.current_y - y_start);
 18650         }
 18651 
 18652       if (PT < CHARPOS (scroll_margin_pos))
 18653         {
 18654           /* Point is in the scroll margin at the top of the window or
 18655              above what is displayed in the window.  */
 18656           int y0, y_to_move;
 18657 
 18658           /* Compute the vertical distance from PT to the scroll
 18659              margin position.  Move as far as scroll_max allows, or
 18660              one screenful, or 10 screen lines, whichever is largest.
 18661              Give up if distance is greater than scroll_max or if we
 18662              didn't reach the scroll margin position.  */
 18663           SET_TEXT_POS (pos, PT, PT_BYTE);
 18664           start_display (&it, w, pos);
 18665           y0 = it.current_y;
 18666           y_to_move = max (it.last_visible_y,
 18667                            max (scroll_max, 10 * frame_line_height));
 18668           move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
 18669                       y_to_move, -1,
 18670                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18671           dy = it.current_y - y0;
 18672           if (dy > scroll_max
 18673               || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
 18674             return SCROLLING_FAILED;
 18675 
 18676           /* Additional scroll for when ZV was too close to point.  */
 18677           dy += y_offset;
 18678 
 18679           /* Compute new window start.  */
 18680           start_display (&it, w, startp);
 18681 
 18682           if (arg_scroll_conservatively)
 18683             amount_to_scroll
 18684               = min (max (dy, frame_line_height),
 18685                      frame_line_height * arg_scroll_conservatively);
 18686           else if (scroll_step || temp_scroll_step)
 18687             amount_to_scroll = scroll_max;
 18688           else
 18689             {
 18690               aggressive = BVAR (current_buffer, scroll_down_aggressively);
 18691               height = WINDOW_BOX_TEXT_HEIGHT (w);
 18692               if (NUMBERP (aggressive))
 18693                 {
 18694                   double float_amount = XFLOATINT (aggressive) * height;
 18695                   int aggressive_scroll = float_amount;
 18696                   if (aggressive_scroll == 0 && float_amount > 0)
 18697                     aggressive_scroll = 1;
 18698                   /* Don't let point enter the scroll margin near
 18699                      bottom of the window, if the value of
 18700                      scroll_down_aggressively happens to be too
 18701                      large.  */
 18702                   if (aggressive_scroll + 2 * this_scroll_margin > height)
 18703                     aggressive_scroll = height - 2 * this_scroll_margin;
 18704                   amount_to_scroll = dy + aggressive_scroll;
 18705                 }
 18706             }
 18707 
 18708           if (amount_to_scroll <= 0)
 18709             return SCROLLING_FAILED;
 18710 
 18711           move_it_vertically_backward (&it, amount_to_scroll);
 18712           startp = it.current.pos;
 18713         }
 18714     }
 18715 
 18716   /* Run window scroll functions.  */
 18717   startp = run_window_scroll_functions (window, startp);
 18718 
 18719   /* Display the window.  Give up if new fonts are loaded, or if point
 18720      doesn't appear.  */
 18721   if (!try_window (window, startp, 0))
 18722     rc = SCROLLING_NEED_LARGER_MATRICES;
 18723   else if (w->cursor.vpos < 0)
 18724     {
 18725       clear_glyph_matrix (w->desired_matrix);
 18726       rc = SCROLLING_FAILED;
 18727     }
 18728   else
 18729     {
 18730       /* Maybe forget recorded base line for line number display.  */
 18731       if (!just_this_one_p
 18732           || current_buffer->clip_changed
 18733           || BEG_UNCHANGED < CHARPOS (startp))
 18734         w->base_line_number = 0;
 18735 
 18736       /* If cursor ends up on a partially visible line,
 18737          treat that as being off the bottom of the screen.  */
 18738       if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
 18739                                         false, false)
 18740           /* It's possible that the cursor is on the first line of the
 18741              buffer, which is partially obscured due to a vscroll
 18742              (Bug#7537).  In that case, avoid looping forever. */
 18743           && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
 18744         {
 18745           clear_glyph_matrix (w->desired_matrix);
 18746           ++extra_scroll_margin_lines;
 18747           goto too_near_end;
 18748         }
 18749       rc = SCROLLING_SUCCESS;
 18750     }
 18751 
 18752   return rc;
 18753 }
 18754 
 18755 
 18756 /* Compute a suitable window start for window W if display of W starts
 18757    on a continuation line.  Value is true if a new window start
 18758    was computed.
 18759 
 18760    The new window start will be computed, based on W's width, starting
 18761    from the start of the continued line.  It is the start of the
 18762    screen line with the minimum distance from the old start W->start,
 18763    which is still before point (otherwise point will definitely not
 18764    be visible in the window).  */
 18765 
 18766 static bool
 18767 compute_window_start_on_continuation_line (struct window *w)
 18768 {
 18769   struct text_pos pos, start_pos, pos_before_pt;
 18770   bool window_start_changed_p = false;
 18771 
 18772   SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 18773 
 18774   /* If window start is on a continuation line...  Window start may be
 18775      < BEGV in case there's invisible text at the start of the
 18776      buffer (M-x rmail, for example).  */
 18777   if (CHARPOS (start_pos) > BEGV
 18778       && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
 18779     {
 18780       struct it it;
 18781       struct glyph_row *row;
 18782 
 18783       /* Handle the case that the window start is out of range.  */
 18784       if (CHARPOS (start_pos) < BEGV)
 18785         SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
 18786       else if (CHARPOS (start_pos) > ZV)
 18787         SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
 18788 
 18789       /* Find the start of the continued line.  This should be fast
 18790          because find_newline is fast (newline cache).  */
 18791       row = w->desired_matrix->rows + window_wants_tab_line (w)
 18792                                     + window_wants_header_line (w);
 18793       init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
 18794                      row, DEFAULT_FACE_ID);
 18795       reseat_at_previous_visible_line_start (&it);
 18796 
 18797       /* Give up (by not using the code in the block below) and say it
 18798          takes too much time to compute a new window start, if the
 18799          line start is "too far" away from the window start.  Also,
 18800          give up if the line start is after point, as in that case
 18801          point will not be visible with any window start we
 18802          compute.  */
 18803       if (IT_CHARPOS (it) <= PT
 18804           && (CHARPOS (start_pos) - IT_CHARPOS (it)
 18805               /* PXW: Do we need upper bounds here?  */
 18806               < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
 18807         {
 18808           int min_distance, distance;
 18809 
 18810           /* Move forward by display lines to find the new window
 18811              start.  If window width was enlarged, the new start can
 18812              be expected to be > the old start.  If window width was
 18813              decreased, the new window start will be < the old start.
 18814              So, we're looking for the display line start with the
 18815              minimum distance from the old window start.  */
 18816           pos_before_pt = pos = it.current.pos;
 18817           min_distance = DISP_INFINITY;
 18818           while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
 18819                  distance < min_distance)
 18820             {
 18821               min_distance = distance;
 18822               if (CHARPOS (pos) <= PT)
 18823                 pos_before_pt = pos;
 18824               pos = it.current.pos;
 18825               if (it.line_wrap == WORD_WRAP)
 18826                 {
 18827                   /* Under WORD_WRAP, move_it_by_lines is likely to
 18828                      overshoot and stop not at the first, but the
 18829                      second character from the left margin.  So in
 18830                      that case, we need a more tight control on the X
 18831                      coordinate of the iterator than move_it_by_lines
 18832                      promises in its contract.  The method is to first
 18833                      go to the last (rightmost) visible character of a
 18834                      line, then move to the leftmost character on the
 18835                      next line in a separate call.  */
 18836                   move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
 18837                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18838                   move_it_to (&it, ZV, 0,
 18839                               it.current_y + it.max_ascent + it.max_descent, -1,
 18840                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18841                 }
 18842               else
 18843                 move_it_by_lines (&it, 1);
 18844             }
 18845 
 18846           /* It makes very little sense to make the new window start
 18847              after point, as point won't be visible.  If that's what
 18848              the loop above finds, fall back on the candidate before
 18849              or at point that is closest to the old window start.  */
 18850           if (CHARPOS (pos) > PT)
 18851             pos = pos_before_pt;
 18852 
 18853           /* Set the window start there.  */
 18854           SET_MARKER_FROM_TEXT_POS (w->start, pos);
 18855           window_start_changed_p = true;
 18856         }
 18857     }
 18858 
 18859   return window_start_changed_p;
 18860 }
 18861 
 18862 
 18863 /* Try cursor movement in case text has not changed in window WINDOW,
 18864    with window start STARTP.  Value is
 18865 
 18866    CURSOR_MOVEMENT_SUCCESS if successful
 18867 
 18868    CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
 18869 
 18870    CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
 18871    display.  *SCROLL_STEP is set to true, under certain circumstances, if
 18872    we want to scroll as if scroll-step were set to 1.  See the code.
 18873 
 18874    CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
 18875    which case we have to abort this redisplay, and adjust matrices
 18876    first.  */
 18877 
 18878 enum
 18879 {
 18880   CURSOR_MOVEMENT_SUCCESS,
 18881   CURSOR_MOVEMENT_CANNOT_BE_USED,
 18882   CURSOR_MOVEMENT_MUST_SCROLL,
 18883   CURSOR_MOVEMENT_NEED_LARGER_MATRICES
 18884 };
 18885 
 18886 static int
 18887 try_cursor_movement (Lisp_Object window, struct text_pos startp,
 18888                      bool *scroll_step)
 18889 {
 18890   struct window *w = XWINDOW (window);
 18891   struct frame *f = XFRAME (w->frame);
 18892   int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
 18893 
 18894 #ifdef GLYPH_DEBUG
 18895   if (inhibit_try_cursor_movement)
 18896     return rc;
 18897 #endif
 18898 
 18899   /* Previously, there was a check for Lisp integer in the
 18900      if-statement below. Now, this field is converted to
 18901      ptrdiff_t, thus zero means invalid position in a buffer.  */
 18902   eassert (w->last_point > 0);
 18903   /* Likewise there was a check whether window_end_vpos is nil or larger
 18904      than the window.  Now window_end_vpos is int and so never nil, but
 18905      let's leave eassert to check whether it fits in the window.  */
 18906   eassert (!w->window_end_valid
 18907            || w->window_end_vpos < w->current_matrix->nrows);
 18908 
 18909   /* Handle case where text has not changed, only point, and it has
 18910      not moved off the frame.  */
 18911   if (/* Point may be in this window.  */
 18912       PT >= CHARPOS (startp)
 18913       /* Selective display hasn't changed.  */
 18914       && !current_buffer->clip_changed
 18915       /* Function force-mode-line-update is used to force a thorough
 18916          redisplay.  It sets either windows_or_buffers_changed or
 18917          update_mode_lines.  So don't take a shortcut here for these
 18918          cases.  */
 18919       && !update_mode_lines
 18920       && !windows_or_buffers_changed
 18921       && !f->cursor_type_changed
 18922       && NILP (Vshow_trailing_whitespace)
 18923       /* When display-line-numbers is in relative mode, moving point
 18924          requires to redraw the entire window.  */
 18925       && !EQ (Vdisplay_line_numbers, Qrelative)
 18926       && !EQ (Vdisplay_line_numbers, Qvisual)
 18927       /* When the current line number should be displayed in a
 18928          distinct face, moving point cannot be handled in optimized
 18929          way as below.  */
 18930       && !(!NILP (Vdisplay_line_numbers)
 18931            && NILP (Finternal_lisp_face_equal_p (Qline_number,
 18932                                                  Qline_number_current_line,
 18933                                                  w->frame)))
 18934       /* This code is not used for mini-buffer for the sake of the case
 18935          of redisplaying to replace an echo area message; since in
 18936          that case the mini-buffer contents per se are usually
 18937          unchanged.  This code is of no real use in the mini-buffer
 18938          since the handling of this_line_start_pos, etc., in redisplay
 18939          handles the same cases.  */
 18940       && !EQ (window, minibuf_window)
 18941       /* When overlay arrow is shown in current buffer, point movement
 18942          is no longer "simple", as it typically causes the overlay
 18943          arrow to move as well.  */
 18944       && !overlay_arrow_in_current_buffer_p ())
 18945     {
 18946       int this_scroll_margin, top_scroll_margin;
 18947       struct glyph_row *row = NULL;
 18948 
 18949 #ifdef GLYPH_DEBUG
 18950       debug_method_add (w, "cursor movement");
 18951 #endif
 18952 
 18953       this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18954 
 18955       top_scroll_margin = this_scroll_margin;
 18956       if (window_wants_tab_line (w))
 18957         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 18958       if (window_wants_header_line (w))
 18959         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 18960 
 18961       /* Start with the row the cursor was displayed during the last
 18962          not paused redisplay.  Give up if that row is not valid.  */
 18963       if (w->last_cursor_vpos < 0
 18964           || w->last_cursor_vpos >= w->current_matrix->nrows)
 18965         rc = CURSOR_MOVEMENT_MUST_SCROLL;
 18966       else
 18967         {
 18968           row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
 18969           /* Skip the tab-line and header-line rows, if any.  */
 18970           if (row->tab_line_p)
 18971             ++row;
 18972           if (row->mode_line_p)
 18973             ++row;
 18974           if (!row->enabled_p)
 18975             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 18976         }
 18977 
 18978       if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
 18979         {
 18980           bool scroll_p = false, must_scroll = false;
 18981           int last_y = window_text_bottom_y (w) - this_scroll_margin;
 18982 
 18983           if (PT > w->last_point)
 18984             {
 18985               /* Point has moved forward.  */
 18986               while (MATRIX_ROW_END_CHARPOS (row) < PT
 18987                      && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 18988                 {
 18989                   eassert (row->enabled_p);
 18990                   ++row;
 18991                 }
 18992 
 18993               /* If the end position of a row equals the start
 18994                  position of the next row, and PT is at that position,
 18995                  we would rather display cursor in the next line.  */
 18996               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 18997                      && MATRIX_ROW_END_CHARPOS (row) == PT
 18998                      && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
 18999                      && MATRIX_ROW_START_CHARPOS (row+1) >= PT
 19000                      && !cursor_row_p (row))
 19001                 ++row;
 19002 
 19003               /* If within the scroll margin, scroll.  Note that
 19004                  MATRIX_ROW_BOTTOM_Y gives the pixel position at which
 19005                  the next line would be drawn, and that
 19006                  this_scroll_margin can be zero.  */
 19007               if (MATRIX_ROW_BOTTOM_Y (row) > last_y
 19008                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19009                   /* Line is completely visible last line in window
 19010                      and PT is to be set in the next line.  */
 19011                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19012                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19013                       && !row->ends_at_zv_p
 19014                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19015                 scroll_p = true;
 19016             }
 19017           else if (PT < w->last_point)
 19018             {
 19019               /* Cursor has to be moved backward.  Note that PT >=
 19020                  CHARPOS (startp) because of the outer if-statement.  */
 19021               struct glyph_row *row0 = row;
 19022 
 19023               while (!row->mode_line_p
 19024                      && (MATRIX_ROW_START_CHARPOS (row) > PT
 19025                          || (MATRIX_ROW_START_CHARPOS (row) == PT
 19026                              && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
 19027                                  || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
 19028                                      row > w->current_matrix->rows
 19029                                      && (row-1)->ends_in_newline_from_string_p))))
 19030                      && (row->y > top_scroll_margin
 19031                          || CHARPOS (startp) == BEGV))
 19032                 {
 19033                   eassert (row->enabled_p);
 19034                   --row;
 19035                 }
 19036 
 19037               /* With bidi-reordered rows we can have buffer positions
 19038                  _decrease_ when going down by rows.  If we haven't
 19039                  found our row in the loop above, give it another try
 19040                  now going in the other direction from the original row.  */
 19041               if (!(MATRIX_ROW_START_CHARPOS (row) <= PT
 19042                     && PT <= MATRIX_ROW_END_CHARPOS (row))
 19043                   && row0->continued_p)
 19044                 {
 19045                   row = row0;
 19046                   while (MATRIX_ROW_START_CHARPOS (row) > PT
 19047                          && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19048                     {
 19049                       eassert (row->enabled_p);
 19050                       ++row;
 19051                     }
 19052                 }
 19053 
 19054               /* Consider the following case: Window starts at BEGV,
 19055                  there is invisible, intangible text at BEGV, so that
 19056                  display starts at some point START > BEGV.  It can
 19057                  happen that we are called with PT somewhere between
 19058                  BEGV and START.  Try to handle that case.  */
 19059               if (row < w->current_matrix->rows
 19060                   || row->mode_line_p)
 19061                 {
 19062                   row = w->current_matrix->rows;
 19063                   /* Skip the tab-line and header-line rows, if any.  */
 19064                   if (row->tab_line_p)
 19065                     ++row;
 19066                   if (row->mode_line_p)
 19067                     ++row;
 19068                 }
 19069 
 19070               /* Due to newlines in overlay strings, we may have to
 19071                  skip forward over overlay strings.  */
 19072               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19073                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19074                      && !cursor_row_p (row))
 19075                 ++row;
 19076 
 19077               /* If within the scroll margin, either the top one or
 19078                  the bottom one, scroll.  */
 19079               if ((row->y < top_scroll_margin
 19080                    && CHARPOS (startp) != BEGV)
 19081                   || MATRIX_ROW_BOTTOM_Y (row) > last_y
 19082                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19083                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19084                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19085                       && !row->ends_at_zv_p
 19086                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19087                 scroll_p = true;
 19088             }
 19089           else
 19090             {
 19091               /* Cursor did not move.  So don't scroll even if cursor line
 19092                  is partially visible, as it was so before.  */
 19093                  rc = CURSOR_MOVEMENT_SUCCESS;
 19094             }
 19095 
 19096           if ((PT < MATRIX_ROW_START_CHARPOS (row)
 19097                && (row == MATRIX_FIRST_TEXT_ROW (w->current_matrix)
 19098                    /* Don't give up if point is inside invisible text
 19099                       at the beginning of its glyph row.  */
 19100                    || (MATRIX_ROW_END_CHARPOS (row-1)
 19101                        == MATRIX_ROW_START_CHARPOS (row))))
 19102               || PT > MATRIX_ROW_END_CHARPOS (row))
 19103             {
 19104               /* if PT is not in the glyph row, give up.  */
 19105               rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19106               must_scroll = true;
 19107             }
 19108           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19109                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19110             {
 19111               struct glyph_row *row1;
 19112 
 19113               /* If rows are bidi-reordered and point moved, back up
 19114                  until we find a row that does not belong to a
 19115                  continuation line.  This is because we must consider
 19116                  all rows of a continued line as candidates for the
 19117                  new cursor positioning, since row start and end
 19118                  positions change non-linearly with vertical position
 19119                  in such rows.  */
 19120               /* FIXME: Revisit this when glyph ``spilling'' in
 19121                  continuation lines' rows is implemented for
 19122                  bidi-reordered rows.  */
 19123               for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 19124                    MATRIX_ROW_CONTINUATION_LINE_P (row);
 19125                    --row)
 19126                 {
 19127                   /* If we hit the beginning of the displayed portion
 19128                      without finding the first row of a continued
 19129                      line, give up.  */
 19130                   if (row <= row1)
 19131                     {
 19132                       rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19133                       break;
 19134                     }
 19135                   eassert (row->enabled_p);
 19136                 }
 19137             }
 19138           if (must_scroll)
 19139             ;
 19140           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19141               && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
 19142               /* Make sure this isn't a header line nor a tab-line by
 19143                  any chance, since then MATRIX_ROW_PARTIALLY_VISIBLE_P
 19144                  might yield true.  */
 19145               && !row->mode_line_p
 19146               && !cursor_row_fully_visible_p (w, true, true, true))
 19147             {
 19148               if (PT == MATRIX_ROW_END_CHARPOS (row)
 19149                   && !row->ends_at_zv_p
 19150                   && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 19151                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19152               else if (row->height > window_box_height (w))
 19153                 {
 19154                   /* If we end up in a partially visible line, let's
 19155                      make it fully visible, except when it's taller
 19156                      than the window, in which case we can't do much
 19157                      about it.  */
 19158                   *scroll_step = true;
 19159                   rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19160                 }
 19161               else
 19162                 {
 19163                   set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 19164                   if (!cursor_row_fully_visible_p (w, false, true, false))
 19165                     rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19166                   else
 19167                     rc = CURSOR_MOVEMENT_SUCCESS;
 19168                 }
 19169             }
 19170           else if (scroll_p)
 19171             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19172           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19173                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19174             {
 19175               /* With bidi-reordered rows, there could be more than
 19176                  one candidate row whose start and end positions
 19177                  occlude point.  We need to let set_cursor_from_row
 19178                  find the best candidate.  */
 19179               /* FIXME: Revisit this when glyph ``spilling'' in
 19180                  continuation lines' rows is implemented for
 19181                  bidi-reordered rows.  */
 19182               bool rv = false;
 19183               bool pt_invis = false;
 19184               Lisp_Object val = get_char_property_and_overlay (make_fixnum (PT),
 19185                                                                Qinvisible,
 19186                                                                Qnil, NULL);
 19187 
 19188               if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 19189                 pt_invis = true;
 19190 
 19191               do
 19192                 {
 19193                   bool at_zv_p = false, exact_match_p = false;
 19194 
 19195                   /* If point is in invisible text, we cannot assume
 19196                      it must be after row's start position, since the
 19197                      row could have invisible text at its beginning
 19198                      where point is located.  */
 19199                   if ((pt_invis || MATRIX_ROW_START_CHARPOS (row) <= PT)
 19200                       && PT <= MATRIX_ROW_END_CHARPOS (row)
 19201                       && cursor_row_p (row))
 19202                     rv |= set_cursor_from_row (w, row, w->current_matrix,
 19203                                                0, 0, 0, 0);
 19204                   /* As soon as we've found the exact match for point,
 19205                      or the first suitable row whose ends_at_zv_p flag
 19206                      is set, we are done.  */
 19207                   if (rv)
 19208                     {
 19209                       at_zv_p = MATRIX_ROW (w->current_matrix,
 19210                                             w->cursor.vpos)->ends_at_zv_p;
 19211                       if (!at_zv_p
 19212                           && w->cursor.hpos >= 0
 19213                           && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
 19214                                                                w->cursor.vpos))
 19215                         {
 19216                           struct glyph_row *candidate =
 19217                             MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 19218                           struct glyph *g =
 19219                             candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
 19220                           ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
 19221 
 19222                           exact_match_p =
 19223                             (BUFFERP (g->object) && g->charpos == PT)
 19224                             || (NILP (g->object)
 19225                                 && (g->charpos == PT
 19226                                     || (g->charpos == 0 && endpos - 1 == PT)));
 19227                           /* Perhaps the point position is inside
 19228                              invisible text?  In that case, we trust
 19229                              'set_cursor_from_row' to do its job and
 19230                              find the best position for the cursor.  */
 19231                           if (!exact_match_p && pt_invis)
 19232                             exact_match_p = true;
 19233                         }
 19234                       if (at_zv_p || exact_match_p)
 19235                         {
 19236                           rc = CURSOR_MOVEMENT_SUCCESS;
 19237                           break;
 19238                         }
 19239                     }
 19240                   if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
 19241                     break;
 19242                   ++row;
 19243                 }
 19244               while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
 19245                        || row->continued_p)
 19246                       && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
 19247                      || (MATRIX_ROW_START_CHARPOS (row) == PT
 19248                          && MATRIX_ROW_BOTTOM_Y (row) < last_y));
 19249               /* If we didn't find any candidate rows, or exited the
 19250                  loop before all the candidates were examined, signal
 19251                  to the caller that this method failed.  */
 19252               if (rc != CURSOR_MOVEMENT_SUCCESS
 19253                   && !(rv
 19254                        && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 19255                        && !row->continued_p))
 19256                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19257               else if (rv)
 19258                 rc = CURSOR_MOVEMENT_SUCCESS;
 19259             }
 19260           else
 19261             {
 19262               do
 19263                 {
 19264                   if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
 19265                     {
 19266                       rc = CURSOR_MOVEMENT_SUCCESS;
 19267                       break;
 19268                     }
 19269                   ++row;
 19270                 }
 19271               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19272                      && MATRIX_ROW_START_CHARPOS (row) == PT
 19273                      && cursor_row_p (row));
 19274             }
 19275         }
 19276     }
 19277 
 19278   return rc;
 19279 }
 19280 
 19281 
 19282 void
 19283 set_vertical_scroll_bar (struct window *w)
 19284 {
 19285   ptrdiff_t start, end, whole;
 19286 
 19287   /* Calculate the start and end positions for the current window.
 19288      At some point, it would be nice to choose between scrollbars
 19289      which reflect the whole buffer size, with special markers
 19290      indicating narrowing, and scrollbars which reflect only the
 19291      visible region.
 19292 
 19293      Note that mini-buffers sometimes aren't displaying any text.  */
 19294   if (!MINI_WINDOW_P (w)
 19295       || (w == XWINDOW (minibuf_window)
 19296           && NILP (echo_area_buffer[0])))
 19297     {
 19298       struct buffer *buf = XBUFFER (w->contents);
 19299 
 19300       whole = BUF_ZV (buf) - BUF_BEGV (buf);
 19301       start = marker_position (w->start) - BUF_BEGV (buf);
 19302       end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
 19303 
 19304       /* If w->window_end_pos cannot be trusted, recompute it "the
 19305          hard way".  But don't bother to be too accurate when
 19306          long-line shortcuts are in effect.  */
 19307       if (!w->window_end_valid && !buf->long_line_optimizations_p)
 19308         {
 19309           struct it it;
 19310           struct text_pos start_pos;
 19311           struct buffer *obuf = current_buffer;
 19312           /* When we display the scroll bar of a mini-window,
 19313              current_buffer is not guaranteed to be the mini-window's
 19314              buffer, see the beginning of redisplay_window.  */
 19315           set_buffer_internal_1 (XBUFFER (w->contents));
 19316           SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19317           start_display (&it, w, start_pos);
 19318           move_it_to (&it, -1, it.last_visible_x, window_box_height (w), -1,
 19319                       MOVE_TO_X | MOVE_TO_Y);
 19320           end -= (BUF_Z (buf) - IT_CHARPOS (it)) - w->window_end_pos;
 19321           set_buffer_internal_1 (obuf);
 19322         }
 19323 
 19324       if (end < start)
 19325         end = start;
 19326       if (whole < (end - start))
 19327         whole = end - start;
 19328     }
 19329   else
 19330     start = end = whole = 0;
 19331 
 19332   /* Indicate what this scroll bar ought to be displaying now.  */
 19333   if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19334     (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19335       (w, end - start, whole, start);
 19336 }
 19337 
 19338 
 19339 void
 19340 set_horizontal_scroll_bar (struct window *w)
 19341 {
 19342   int start, end, whole, portion;
 19343 
 19344   if (!MINI_WINDOW_P (w) || EQ (w->horizontal_scroll_bar_type, Qbottom))
 19345     {
 19346       struct buffer *b = XBUFFER (w->contents);
 19347       struct buffer *old_buffer = NULL;
 19348       struct it it;
 19349       struct text_pos startp;
 19350 
 19351       if (b != current_buffer)
 19352         {
 19353           old_buffer = current_buffer;
 19354           set_buffer_internal (b);
 19355         }
 19356 
 19357       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19358       start_display (&it, w, startp);
 19359       it.last_visible_x = INT_MAX;
 19360       whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
 19361                           MOVE_TO_X | MOVE_TO_Y);
 19362       /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
 19363                           window_box_height (w), -1,
 19364                           MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
 19365 
 19366       start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
 19367       end = start + window_box_width (w, TEXT_AREA);
 19368       portion = end - start;
 19369       /* After enlarging a horizontally scrolled window such that it
 19370          gets at least as wide as the text it contains, make sure that
 19371          the thumb doesn't fill the entire scroll bar so we can still
 19372          drag it back to see the entire text.  */
 19373       whole = max (whole, end);
 19374 
 19375       if (it.bidi_p)
 19376         {
 19377           Lisp_Object pdir;
 19378 
 19379           pdir = Fcurrent_bidi_paragraph_direction (Qnil);
 19380           if (EQ (pdir, Qright_to_left))
 19381             {
 19382               start = whole - end;
 19383               end = start + portion;
 19384             }
 19385         }
 19386 
 19387       if (old_buffer)
 19388         set_buffer_internal (old_buffer);
 19389     }
 19390   else
 19391     start = end = whole = portion = 0;
 19392 
 19393   w->hscroll_whole = whole;
 19394 
 19395   /* Indicate what this scroll bar ought to be displaying now.  */
 19396   if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19397     (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19398       (w, portion, whole, start);
 19399 }
 19400 
 19401 /* Subroutine of redisplay_window, to determine whether a window-start
 19402    point STARTP of WINDOW should be rejected.  */
 19403 static bool
 19404 window_start_acceptable_p (Lisp_Object window, ptrdiff_t startp)
 19405 {
 19406   if (!make_window_start_visible)
 19407     return true;
 19408 
 19409   struct window *w = XWINDOW (window);
 19410   struct frame *f = XFRAME (w->frame);
 19411   Lisp_Object startpos = make_fixnum (startp);
 19412   Lisp_Object invprop, disp_spec;
 19413   struct text_pos ignored;
 19414 
 19415   /* Is STARTP in invisible text?  */
 19416   if ((invprop = Fget_char_property (startpos, Qinvisible, window)),
 19417       TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19418     return false;
 19419 
 19420   /* Is STARTP covered by a replacing 'display' property?  */
 19421   if (!NILP (disp_spec = Fget_char_property (startpos, Qdisplay, window))
 19422       && handle_display_spec (NULL, disp_spec, Qnil, Qnil, &ignored, startp,
 19423                               FRAME_WINDOW_P (f)) > 0)
 19424     return false;
 19425 
 19426   return true;
 19427 }
 19428 
 19429 DEFUN ("long-line-optimizations-p", Flong_line_optimizations_p, Slong_line_optimizations_p,
 19430        0, 0, 0,
 19431        doc: /* Return non-nil if long-line optimizations are in effect in current buffer.
 19432 See `long-line-threshold' and `large-hscroll-threshold' for what these
 19433 optimizations mean and when they are in effect.  */)
 19434   (void)
 19435 {
 19436   return current_buffer->long_line_optimizations_p ? Qt : Qnil;
 19437 }
 19438 
 19439 /* Redisplay leaf window WINDOW.  JUST_THIS_ONE_P means only
 19440    selected_window is redisplayed.
 19441 
 19442    We can return without actually redisplaying the window if fonts has been
 19443    changed on window's frame.  In that case, redisplay_internal will retry.
 19444 
 19445    As one of the important parts of redisplaying a window, we need to
 19446    decide whether the previous window-start position (stored in the
 19447    window's w->start marker position) is still valid, and if it isn't,
 19448    recompute it.  Some details about that:
 19449 
 19450     . The previous window-start could be in a continuation line, in
 19451       which case we need to recompute it when the window width
 19452       changes.  See compute_window_start_on_continuation_line and its
 19453       call below.
 19454 
 19455     . The text that changed since last redisplay could include the
 19456       previous window-start position.  In that case, we try to salvage
 19457       what we can from the current glyph matrix by calling
 19458       try_scrolling, which see.
 19459 
 19460     . Some Emacs command could force us to use a specific window-start
 19461       position by setting the window's force_start flag, or gently
 19462       propose doing that by setting the window's optional_new_start
 19463       flag.  In these cases, we try using the specified start point if
 19464       that succeeds (i.e. the window desired matrix is successfully
 19465       recomputed, and point location is within the window).  In case
 19466       of optional_new_start, we first check if the specified start
 19467       position is feasible, i.e. if it will allow point to be
 19468       displayed in the window.  If using the specified start point
 19469       fails, e.g., if new fonts are needed to be loaded, we abort the
 19470       redisplay cycle and leave it up to the next cycle to figure out
 19471       things.
 19472 
 19473     . Note that the window's force_start flag is sometimes set by
 19474       redisplay itself, when it decides that the previous window start
 19475       point is fine and should be kept.  Search for "goto force_start"
 19476       below to see the details.  Like the values of window-start
 19477       specified outside of redisplay, these internally-deduced values
 19478       are tested for feasibility, and ignored if found to be
 19479       unfeasible.
 19480 
 19481     . Note that the function try_window, used to completely redisplay
 19482       a window, accepts the window's start point as its argument.
 19483       This is used several times in the redisplay code to control
 19484       where the window start will be, according to user options such
 19485       as scroll-conservatively, and also to ensure the screen line
 19486       showing point will be fully (as opposed to partially) visible on
 19487       display.  */
 19488 
 19489 static void
 19490 redisplay_window (Lisp_Object window, bool just_this_one_p)
 19491 {
 19492   struct window *w = XWINDOW (window);
 19493   struct frame *f = XFRAME (w->frame);
 19494   struct buffer *buffer = XBUFFER (w->contents);
 19495   struct buffer *old = current_buffer;
 19496   struct text_pos lpoint, opoint, startp;
 19497   bool update_mode_line;
 19498   int tem;
 19499   struct it it;
 19500   /* Record it now because it's overwritten.  */
 19501   bool current_matrix_up_to_date_p = false;
 19502   bool used_current_matrix_p = false;
 19503   /* This is less strict than current_matrix_up_to_date_p.
 19504      It indicates that the buffer contents and narrowing are unchanged.  */
 19505   bool buffer_unchanged_p = false;
 19506   bool temp_scroll_step = false;
 19507   specpdl_ref count = SPECPDL_INDEX ();
 19508   int rc;
 19509   int centering_position = -1;
 19510   bool last_line_misfit = false;
 19511   ptrdiff_t beg_unchanged, end_unchanged;
 19512   int frame_line_height, margin;
 19513   bool use_desired_matrix;
 19514   void *itdata = NULL;
 19515 
 19516   SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19517   opoint = lpoint;
 19518 
 19519 #ifdef GLYPH_DEBUG
 19520   *w->desired_matrix->method = 0;
 19521 #endif
 19522 
 19523   if (!just_this_one_p && needs_no_redisplay (w))
 19524     return;
 19525 
 19526   /* Make sure that both W's markers are valid.  */
 19527   eassert (XMARKER (w->start)->buffer == buffer);
 19528   eassert (XMARKER (w->pointm)->buffer == buffer);
 19529 
 19530   reconsider_clip_changes (w);
 19531   frame_line_height = default_line_pixel_height (w);
 19532   margin = window_scroll_margin (w, MARGIN_IN_LINES);
 19533 
 19534 
 19535   /* Has the mode line to be updated?  */
 19536   update_mode_line = (w->update_mode_line
 19537                       || update_mode_lines
 19538                       || buffer->clip_changed
 19539                       || buffer->prevent_redisplay_optimizations_p);
 19540 
 19541   if (!just_this_one_p)
 19542     /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
 19543        cleverly elsewhere.  */
 19544     w->must_be_updated_p = true;
 19545 
 19546   if (MINI_WINDOW_P (w))
 19547     {
 19548       if (w == XWINDOW (echo_area_window)
 19549           && !NILP (echo_area_buffer[0]))
 19550         {
 19551           if (update_mode_line)
 19552             /* We may have to update a tty frame's menu bar or a
 19553                tool-bar.  Example `M-x C-h C-h C-g'.  */
 19554             goto finish_menu_bars;
 19555           else
 19556             /* We've already displayed the echo area glyphs in this window.  */
 19557             goto finish_scroll_bars;
 19558         }
 19559       else if ((w != XWINDOW (minibuf_window)
 19560                 || minibuf_level == 0)
 19561                /* When buffer is nonempty, redisplay window normally.  */
 19562                && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
 19563                /* Quail displays non-mini buffers in minibuffer window.
 19564                   In that case, redisplay the window normally.  */
 19565                && !NILP (Fmemq (w->contents, Vminibuffer_list)))
 19566         {
 19567           /* W is a mini-buffer window, but it's not active, so clear
 19568              it.  */
 19569           int yb = window_text_bottom_y (w);
 19570           struct glyph_row *row;
 19571           int y;
 19572 
 19573           for (y = 0, row = w->desired_matrix->rows;
 19574                y < yb;
 19575                y += row->height, ++row)
 19576             blank_row (w, row, y);
 19577           goto finish_scroll_bars;
 19578         }
 19579       else if (minibuf_level >= 1)
 19580         {
 19581           /* We could have a message produced by set-minibuffer-message
 19582              displayed in the mini-window as an overlay, so resize the
 19583              mini-window if needed.  */
 19584           resize_mini_window (w, false);
 19585         }
 19586 
 19587       clear_glyph_matrix (w->desired_matrix);
 19588     }
 19589 
 19590   /* Otherwise set up data on this window; select its buffer and point
 19591      value.  */
 19592   /* Really select the buffer, for the sake of buffer-local
 19593      variables.  */
 19594   set_buffer_internal_1 (XBUFFER (w->contents));
 19595 
 19596   current_matrix_up_to_date_p
 19597     = (w->window_end_valid
 19598        && !current_buffer->clip_changed
 19599        && !current_buffer->prevent_redisplay_optimizations_p
 19600        && !window_outdated (w)
 19601        && !composition_break_at_point
 19602        && !hscrolling_current_line_p (w));
 19603 
 19604   beg_unchanged = BEG_UNCHANGED;
 19605   end_unchanged = END_UNCHANGED;
 19606 
 19607   SET_TEXT_POS (opoint, PT, PT_BYTE);
 19608 
 19609   specbind (Qinhibit_point_motion_hooks, Qt);
 19610 
 19611   buffer_unchanged_p
 19612     = (w->window_end_valid
 19613        && !current_buffer->clip_changed
 19614        && !window_outdated (w));
 19615 
 19616   /* When windows_or_buffers_changed is non-zero, we can't rely
 19617      on the window end being valid, so set it to zero there.  */
 19618   if (windows_or_buffers_changed)
 19619     {
 19620       /* If window starts on a continuation line, maybe adjust the
 19621          window start in case the window's width changed.  */
 19622       if (XMARKER (w->start)->buffer == current_buffer)
 19623         compute_window_start_on_continuation_line (w);
 19624 
 19625       w->window_end_valid = false;
 19626       /* If so, we also can't rely on current matrix
 19627          and should not fool try_cursor_movement below.  */
 19628       current_matrix_up_to_date_p = false;
 19629     }
 19630 
 19631   /* Some sanity checks.  */
 19632   CHECK_WINDOW_END (w);
 19633   if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
 19634     emacs_abort ();
 19635   if (BYTEPOS (opoint) < CHARPOS (opoint))
 19636     emacs_abort ();
 19637 
 19638   if (mode_line_update_needed (w))
 19639     update_mode_line = true;
 19640 
 19641   /* Point refers normally to the selected window.  For any other
 19642      window, set up appropriate value.  */
 19643   if (!EQ (window, selected_window))
 19644     {
 19645       ptrdiff_t new_pt = marker_position (w->pointm);
 19646       ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
 19647 
 19648       if (new_pt < BEGV)
 19649         {
 19650           new_pt = BEGV;
 19651           new_pt_byte = BEGV_BYTE;
 19652           set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
 19653         }
 19654       else if (new_pt > (ZV - 1))
 19655         {
 19656           new_pt = ZV;
 19657           new_pt_byte = ZV_BYTE;
 19658           set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
 19659         }
 19660 
 19661       /* We don't use SET_PT so that the point-motion hooks don't run.  */
 19662       TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
 19663     }
 19664 
 19665   /* If any of the character widths specified in the display table
 19666      have changed, invalidate the width run cache.  It's true that
 19667      this may be a bit late to catch such changes, but the rest of
 19668      redisplay goes (non-fatally) haywire when the display table is
 19669      changed, so why should we worry about doing any better?  */
 19670   if (current_buffer->width_run_cache
 19671       || (current_buffer->base_buffer
 19672           && current_buffer->base_buffer->width_run_cache))
 19673     {
 19674       struct Lisp_Char_Table *disptab = buffer_display_table ();
 19675 
 19676       if (! disptab_matches_widthtab
 19677           (disptab, XVECTOR (BVAR (current_buffer, width_table))))
 19678         {
 19679           struct buffer *buf = current_buffer;
 19680 
 19681           if (buf->base_buffer)
 19682             buf = buf->base_buffer;
 19683           invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
 19684           recompute_width_table (current_buffer, disptab);
 19685         }
 19686     }
 19687 
 19688   /* Check whether the buffer to be displayed contains long lines.  */
 19689   if (!NILP (Vlong_line_threshold)
 19690       && !current_buffer->long_line_optimizations_p
 19691       && (CHARS_MODIFF - UNCHANGED_MODIFIED > 8
 19692           || current_buffer->clip_changed))
 19693     {
 19694       ptrdiff_t cur, next, found, max = 0, threshold;
 19695       threshold = XFIXNUM (Vlong_line_threshold);
 19696       for (cur = BEGV; cur < ZV; cur = next)
 19697         {
 19698           next = find_newline1 (cur, CHAR_TO_BYTE (cur), 0, -1, 1,
 19699                                 &found, NULL, true);
 19700           if (next - cur > max) max = next - cur;
 19701           if (!found || max > threshold) break;
 19702         }
 19703       if (max > threshold)
 19704         current_buffer->long_line_optimizations_p = true;
 19705     }
 19706 
 19707   /* If window-start is screwed up, choose a new one.  */
 19708   if (XMARKER (w->start)->buffer != current_buffer)
 19709     goto recenter;
 19710 
 19711   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19712 
 19713   /* If someone specified a new starting point but did not insist,
 19714      check whether it can be used.  */
 19715   if ((w->optional_new_start || window_frozen_p (w))
 19716       && CHARPOS (startp) >= BEGV
 19717       && CHARPOS (startp) <= ZV)
 19718     {
 19719       ptrdiff_t it_charpos;
 19720 
 19721       w->optional_new_start = false;
 19722       if (!w->force_start)
 19723         {
 19724           start_display (&it, w, startp);
 19725           move_it_to (&it, PT, 0, it.last_visible_y, -1,
 19726                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19727           /* Record IT's position now, since line_bottom_y might
 19728              change that.  */
 19729           it_charpos = IT_CHARPOS (it);
 19730           /* Make sure we set the force_start flag only if the cursor
 19731              row will be fully visible.  Otherwise, the code under
 19732              force_start label below will try to move point back into
 19733              view, which is not what the code which sets
 19734              optional_new_start wants.  */
 19735           if (it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
 19736             {
 19737               if (it_charpos == PT)
 19738                 w->force_start = true;
 19739               /* IT may overshoot PT if text at PT is invisible.  */
 19740               else if (it_charpos > PT && CHARPOS (startp) <= PT)
 19741                 w->force_start = true;
 19742 #ifdef GLYPH_DEBUG
 19743               if (w->force_start)
 19744                 {
 19745                   if (window_frozen_p (w))
 19746                     debug_method_add (w, "set force_start from frozen window start");
 19747                   else
 19748                     debug_method_add (w, "set force_start from optional_new_start");
 19749                 }
 19750 #endif
 19751             }
 19752         }
 19753     }
 19754 
 19755  force_start:
 19756 
 19757   /* Handle case where place to start displaying has been specified,
 19758      unless the specified location is outside the accessible range.  */
 19759   if (w->force_start)
 19760     {
 19761       /* We set this later on if we have to adjust point.  */
 19762       int new_vpos = -1;
 19763 
 19764       w->force_start = false;
 19765 
 19766       /* The vscroll should be preserved in this case, since
 19767          `pixel-scroll-precision-mode' must continue working normally
 19768          when a mini-window is resized.  (bug#55312) */
 19769       if (!w->preserve_vscroll_p || !window_frozen_p (w))
 19770         w->vscroll = 0;
 19771 
 19772       w->preserve_vscroll_p = false;
 19773       w->window_end_valid = false;
 19774 
 19775       /* Forget any recorded base line for line number display.  */
 19776       if (!buffer_unchanged_p)
 19777         w->base_line_number = 0;
 19778 
 19779       /* Redisplay the mode line.  Select the buffer properly for that.
 19780          Also, run the hook window-scroll-functions
 19781          because we have scrolled.  */
 19782       /* Note, we do this after clearing force_start because
 19783          if there's an error, it is better to forget about force_start
 19784          than to get into an infinite loop calling the hook functions
 19785          and having them get more errors.  */
 19786       if (!update_mode_line
 19787           || ! NILP (Vwindow_scroll_functions))
 19788         {
 19789           update_mode_line = true;
 19790           w->update_mode_line = true;
 19791           startp = run_window_scroll_functions (window, startp);
 19792         }
 19793 
 19794       if (CHARPOS (startp) < BEGV)
 19795         SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
 19796       else if (CHARPOS (startp) > ZV)
 19797         SET_TEXT_POS (startp, ZV, ZV_BYTE);
 19798 
 19799       /* Reject the specified start location if it is invisible, and
 19800          the buffer wants it always visible.  */
 19801       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 19802         goto ignore_start;
 19803 
 19804       /* Redisplay, then check if cursor has been set during the
 19805          redisplay.  Give up if new fonts were loaded.  */
 19806       /* We used to issue a CHECK_MARGINS argument to try_window here,
 19807          but this causes scrolling to fail when point begins inside
 19808          the scroll margin (bug#148) -- cyd  */
 19809       clear_glyph_matrix (w->desired_matrix);
 19810       if (!try_window (window, startp, 0))
 19811         {
 19812           w->force_start = true;
 19813           clear_glyph_matrix (w->desired_matrix);
 19814           goto need_larger_matrices;
 19815         }
 19816 
 19817       if (w->cursor.vpos < 0)
 19818         {
 19819           /* If point does not appear, try to move point so it does
 19820              appear.  The desired matrix has been built above, so we
 19821              can use it here.  First see if point is in invisible
 19822              text, and if so, move it to the first visible buffer
 19823              position past that.  */
 19824           struct glyph_row *r = NULL;
 19825           Lisp_Object invprop =
 19826             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 19827                                            Qnil, NULL);
 19828 
 19829           if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19830             {
 19831               ptrdiff_t alt_pt;
 19832               Lisp_Object invprop_end =
 19833                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 19834                                                    Qnil, Qnil);
 19835 
 19836               if (FIXNATP (invprop_end))
 19837                 alt_pt = XFIXNAT (invprop_end);
 19838               else
 19839                 alt_pt = ZV;
 19840               r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
 19841                                       NULL, 0);
 19842             }
 19843           if (r)
 19844             new_vpos = MATRIX_ROW_BOTTOM_Y (r);
 19845           else  /* Give up and just move to the middle of the window.  */
 19846             new_vpos = window_box_height (w) / 2;
 19847         }
 19848 
 19849       if (!cursor_row_fully_visible_p (w, false, false, false))
 19850         {
 19851           /* Point does appear, but on a line partly visible at end of window.
 19852              Move it back to a fully-visible line.  */
 19853           new_vpos = window_box_height (w);
 19854           /* But if window_box_height suggests a Y coordinate that is
 19855              not less than we already have, that line will clearly not
 19856              be fully visible, so give up and scroll the display.
 19857              This can happen when the default face uses a font whose
 19858              dimensions are different from the frame's default
 19859              font.  */
 19860           if (new_vpos >= w->cursor.y)
 19861             {
 19862               w->cursor.vpos = -1;
 19863               clear_glyph_matrix (w->desired_matrix);
 19864               goto try_to_scroll;
 19865             }
 19866         }
 19867       else if (w->cursor.vpos >= 0)
 19868         {
 19869           /* Some people insist on not letting point enter the scroll
 19870              margin, even though this part handles windows that didn't
 19871              scroll at all.  */
 19872           int pixel_margin = margin * frame_line_height;
 19873           bool tab_line = window_wants_tab_line (w);
 19874           bool header_line = window_wants_header_line (w);
 19875 
 19876           /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
 19877              below, which finds the row to move point to, advances by
 19878              the Y coordinate of the _next_ row, see the definition of
 19879              MATRIX_ROW_BOTTOM_Y.  */
 19880           if (w->cursor.vpos < margin + tab_line + header_line)
 19881             {
 19882               w->cursor.vpos = -1;
 19883               clear_glyph_matrix (w->desired_matrix);
 19884               goto try_to_scroll;
 19885             }
 19886           else
 19887             {
 19888               int window_height = window_box_height (w);
 19889 
 19890               if (tab_line)
 19891                 window_height += CURRENT_TAB_LINE_HEIGHT (w);
 19892               if (header_line)
 19893                 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
 19894               if (w->cursor.y >= window_height - pixel_margin)
 19895                 {
 19896                   w->cursor.vpos = -1;
 19897                   clear_glyph_matrix (w->desired_matrix);
 19898                   goto try_to_scroll;
 19899                 }
 19900             }
 19901         }
 19902 
 19903       /* If we need to move point for either of the above reasons,
 19904          now actually do it.  */
 19905       if (new_vpos >= 0)
 19906         {
 19907           struct glyph_row *row;
 19908 
 19909           row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
 19910           while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos
 19911                  && !row->ends_at_zv_p)
 19912             ++row;
 19913 
 19914           TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
 19915                             MATRIX_ROW_START_BYTEPOS (row));
 19916 
 19917           if (w != XWINDOW (selected_window))
 19918             set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
 19919           else if (current_buffer == old)
 19920             SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19921 
 19922           set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
 19923 
 19924           /* Re-run pre-redisplay-function so it can update the region
 19925              according to the new position of point.  */
 19926           /* Other than the cursor, w's redisplay is done so we can set its
 19927              redisplay to false.  Also the buffer's redisplay can be set to
 19928              false, since propagate_buffer_redisplay should have already
 19929              propagated its info to `w' anyway.  */
 19930           w->redisplay = false;
 19931           XBUFFER (w->contents)->text->redisplay = false;
 19932           safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
 19933 
 19934           if (w->redisplay || XBUFFER (w->contents)->text->redisplay
 19935               || ((EQ (Vdisplay_line_numbers, Qrelative)
 19936                    || EQ (Vdisplay_line_numbers, Qvisual))
 19937                   && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
 19938             {
 19939               /* Either pre-redisplay-function made changes (e.g. move
 19940                  the region), or we moved point in a window that is
 19941                  under display-line-numbers = relative mode.  We need
 19942                  another round of redisplay.  */
 19943               clear_glyph_matrix (w->desired_matrix);
 19944               if (!try_window (window, startp, 0))
 19945                 goto need_larger_matrices;
 19946             }
 19947         }
 19948       if (w->cursor.vpos < 0
 19949           || !cursor_row_fully_visible_p (w, false, false, false))
 19950         {
 19951           clear_glyph_matrix (w->desired_matrix);
 19952           goto try_to_scroll;
 19953         }
 19954 
 19955 #ifdef GLYPH_DEBUG
 19956       debug_method_add (w, "forced window start");
 19957 #endif
 19958       goto done;
 19959     }
 19960 
 19961  ignore_start:
 19962 
 19963   /* Handle case where text has not changed, only point, and it has
 19964      not moved off the frame, and we are not retrying after hscroll.
 19965      (current_matrix_up_to_date_p is true when retrying.)  */
 19966   if (current_matrix_up_to_date_p
 19967       && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
 19968           rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
 19969     {
 19970       switch (rc)
 19971         {
 19972         case CURSOR_MOVEMENT_SUCCESS:
 19973           used_current_matrix_p = true;
 19974           goto done;
 19975 
 19976         case CURSOR_MOVEMENT_MUST_SCROLL:
 19977           goto try_to_scroll;
 19978 
 19979         default:
 19980           emacs_abort ();
 19981         }
 19982     }
 19983   /* If current starting point was originally the beginning of a line
 19984      but no longer is, or if the starting point is invisible but the
 19985      buffer wants it always visible, find a new starting point.  */
 19986   else if (w->start_at_line_beg
 19987            && ((CHARPOS (startp) > BEGV
 19988                 && FETCH_BYTE (BYTEPOS (startp) - 1) != '\n')
 19989                || (CHARPOS (startp) >= BEGV
 19990                    && CHARPOS (startp) <= ZV
 19991                    && !window_start_acceptable_p (window, CHARPOS (startp)))))
 19992     {
 19993 #ifdef GLYPH_DEBUG
 19994       debug_method_add (w, "recenter 1");
 19995 #endif
 19996       goto recenter;
 19997     }
 19998 
 19999   /* Try scrolling with try_window_id.  Value is > 0 if update has
 20000      been done, it is -1 if we know that the same window start will
 20001      not work.  It is 0 if unsuccessful for some other reason.  */
 20002   else if ((tem = try_window_id (w)) != 0)
 20003     {
 20004 #ifdef GLYPH_DEBUG
 20005       debug_method_add (w, "try_window_id %d", tem);
 20006 #endif
 20007 
 20008       if (f->fonts_changed)
 20009         goto need_larger_matrices;
 20010       if (tem > 0)
 20011         goto done;
 20012 
 20013       /* Otherwise try_window_id has returned -1 which means that we
 20014          don't want the alternative below this comment to execute.  */
 20015     }
 20016   else if (CHARPOS (startp) >= BEGV
 20017            && CHARPOS (startp) <= ZV
 20018            && PT >= CHARPOS (startp)
 20019            && (CHARPOS (startp) < ZV
 20020                /* Avoid starting at end of buffer.  */
 20021                || CHARPOS (startp) == BEGV
 20022                || !window_outdated (w)))
 20023     {
 20024       int d1, d2, d5, d6;
 20025       int rtop, rbot;
 20026 
 20027       /* If first window line is a continuation line, and window start
 20028          is inside the modified region, but the first change is before
 20029          current window start, we must select a new window start.
 20030 
 20031          However, if this is the result of a down-mouse event (e.g. by
 20032          extending the mouse-drag-overlay), we don't want to select a
 20033          new window start, since that would change the position under
 20034          the mouse, resulting in an unwanted mouse-movement rather
 20035          than a simple mouse-click.  */
 20036       if (!w->start_at_line_beg
 20037           && NILP (track_mouse)
 20038           && CHARPOS (startp) > BEGV
 20039           && CHARPOS (startp) > BEG + beg_unchanged
 20040           && CHARPOS (startp) <= Z - end_unchanged
 20041           /* Even if w->start_at_line_beg is nil, a new window may
 20042              start at a line_beg, since that's how set_buffer_window
 20043              sets it.  So, we need to check the return value of
 20044              compute_window_start_on_continuation_line.  (See also
 20045              bug#197).  */
 20046           && XMARKER (w->start)->buffer == current_buffer
 20047           && compute_window_start_on_continuation_line (w)
 20048           /* It doesn't make sense to force the window start like we
 20049              do at label force_start if it is already known that point
 20050              will not be fully visible in the resulting window, because
 20051              doing so will move point from its correct position
 20052              instead of scrolling the window to bring point into view.
 20053              See bug#9324.  */
 20054           && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
 20055           /* A very tall row could need more than the window height,
 20056              in which case we accept that it is partially visible.  */
 20057           && (rtop != 0) == (rbot != 0))
 20058         {
 20059           w->force_start = true;
 20060           SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20061 #ifdef GLYPH_DEBUG
 20062           debug_method_add (w, "recomputed window start in continuation line");
 20063 #endif
 20064           goto force_start;
 20065         }
 20066 
 20067       /* Don't use the same window-start if it is invisible or covered
 20068          by a replacing 'display' property and the buffer requested
 20069          the window-start to be always visible.  */
 20070       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20071         {
 20072 #ifdef GLYPH_DEBUG
 20073           debug_method_add (w, "recenter 2");
 20074 #endif
 20075           goto recenter;
 20076         }
 20077 
 20078 #ifdef GLYPH_DEBUG
 20079       debug_method_add (w, "same window start");
 20080 #endif
 20081 
 20082       /* Try to redisplay starting at same place as before.
 20083          If point has not moved off frame, accept the results.  */
 20084       if (!current_matrix_up_to_date_p
 20085           /* Don't use try_window_reusing_current_matrix in this case
 20086              because a window scroll function can have changed the
 20087              buffer.  */
 20088           || !NILP (Vwindow_scroll_functions)
 20089           || MINI_WINDOW_P (w)
 20090           || !(used_current_matrix_p
 20091                = try_window_reusing_current_matrix (w)))
 20092         {
 20093           IF_DEBUG (debug_method_add (w, "1"));
 20094           clear_glyph_matrix (w->desired_matrix);
 20095           if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
 20096             /* -1 means we need to scroll.
 20097                0 means we need new matrices, but fonts_changed
 20098                is set in that case, so we will detect it below.  */
 20099             goto try_to_scroll;
 20100         }
 20101 
 20102       if (f->fonts_changed)
 20103         goto need_larger_matrices;
 20104 
 20105       if (w->cursor.vpos >= 0)
 20106         {
 20107           if (!just_this_one_p
 20108               || current_buffer->clip_changed
 20109               || BEG_UNCHANGED < CHARPOS (startp))
 20110             /* Forget any recorded base line for line number display.  */
 20111             w->base_line_number = 0;
 20112 
 20113           if (!cursor_row_fully_visible_p (w, true, false, false))
 20114             {
 20115               clear_glyph_matrix (w->desired_matrix);
 20116               last_line_misfit = true;
 20117             }
 20118             /* Drop through and scroll.  */
 20119           else
 20120             goto done;
 20121         }
 20122       else
 20123         clear_glyph_matrix (w->desired_matrix);
 20124     }
 20125 
 20126  try_to_scroll:
 20127 
 20128   /* Redisplay the mode line.  Select the buffer properly for that.  */
 20129   if (!update_mode_line)
 20130     {
 20131       update_mode_line = true;
 20132       w->update_mode_line = true;
 20133     }
 20134 
 20135   /* Try to scroll by specified few lines.  */
 20136   if ((0 < scroll_conservatively
 20137        /* FIXME: the option is supposed to affect minibuffers, but we
 20138           test MINI_WINDOW_P, which can also catch uses of
 20139           mini-windows for displaying the echo area.  Do we need to
 20140           distinguish these two use cases?  */
 20141        || (scroll_minibuffer_conservatively && MINI_WINDOW_P (w))
 20142        || 0 < emacs_scroll_step
 20143        || temp_scroll_step
 20144        || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
 20145        || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
 20146       && CHARPOS (startp) >= BEGV
 20147       && CHARPOS (startp) <= ZV)
 20148     {
 20149       /* The function returns -1 if new fonts were loaded, 1 if
 20150          successful, 0 if not successful.  */
 20151       int ss = try_scrolling (window, just_this_one_p,
 20152                               ((scroll_minibuffer_conservatively
 20153                                 && MINI_WINDOW_P (w))
 20154                                ? SCROLL_LIMIT + 1
 20155                                : scroll_conservatively),
 20156                               emacs_scroll_step,
 20157                               temp_scroll_step, last_line_misfit);
 20158       switch (ss)
 20159         {
 20160         case SCROLLING_SUCCESS:
 20161           goto done;
 20162 
 20163         case SCROLLING_NEED_LARGER_MATRICES:
 20164           goto need_larger_matrices;
 20165 
 20166         case SCROLLING_FAILED:
 20167           break;
 20168 
 20169         default:
 20170           emacs_abort ();
 20171         }
 20172     }
 20173 
 20174   /* Finally, just choose a place to start which positions point
 20175      according to user preferences.  */
 20176 
 20177  recenter:
 20178 
 20179 #ifdef GLYPH_DEBUG
 20180   debug_method_add (w, "recenter");
 20181 #endif
 20182 
 20183   /* Forget any previously recorded base line for line number display.  */
 20184   if (!buffer_unchanged_p)
 20185     w->base_line_number = 0;
 20186 
 20187   /* Determine the window start relative to point.  */
 20188   init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20189   it.current_y = it.last_visible_y;
 20190   if (centering_position < 0)
 20191     {
 20192       ptrdiff_t margin_pos = CHARPOS (startp);
 20193       Lisp_Object aggressive;
 20194       bool scrolling_up;
 20195 
 20196       /* If there is a scroll margin at the top of the window, find
 20197          its character position.  */
 20198       if (margin
 20199           /* Cannot call start_display if startp is not in the
 20200              accessible region of the buffer.  This can happen when we
 20201              have just switched to a different buffer and/or changed
 20202              its restriction.  In that case, startp is initialized to
 20203              the character position 1 (BEGV) because we did not yet
 20204              have chance to display the buffer even once.  */
 20205           && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
 20206         {
 20207           struct it it1;
 20208           void *it1data = NULL;
 20209 
 20210           SAVE_IT (it1, it, it1data);
 20211           start_display (&it1, w, startp);
 20212           move_it_vertically (&it1, margin * frame_line_height);
 20213           margin_pos = IT_CHARPOS (it1);
 20214           RESTORE_IT (&it, &it, it1data);
 20215         }
 20216       scrolling_up = PT > margin_pos;
 20217       aggressive =
 20218         scrolling_up
 20219         ? BVAR (current_buffer, scroll_up_aggressively)
 20220         : BVAR (current_buffer, scroll_down_aggressively);
 20221 
 20222       if (!MINI_WINDOW_P (w)
 20223           && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
 20224         {
 20225           int pt_offset = 0;
 20226 
 20227           /* Setting scroll-conservatively overrides
 20228              scroll-*-aggressively.  */
 20229           if (!scroll_conservatively && NUMBERP (aggressive))
 20230             {
 20231               double float_amount = XFLOATINT (aggressive);
 20232 
 20233               pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
 20234               if (pt_offset == 0 && float_amount > 0)
 20235                 pt_offset = 1;
 20236               if (pt_offset && margin > 0)
 20237                 margin -= 1;
 20238             }
 20239           /* Compute how much to move the window start backward from
 20240              point so that point will be displayed where the user
 20241              wants it.  */
 20242           if (scrolling_up)
 20243             {
 20244               centering_position = it.last_visible_y;
 20245               if (pt_offset)
 20246                 centering_position -= pt_offset;
 20247               centering_position -=
 20248                 (frame_line_height * (1 + margin + last_line_misfit)
 20249                  + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 20250               /* Don't let point enter the scroll margin near top of
 20251                  the window.  */
 20252               if (centering_position < margin * frame_line_height)
 20253                 centering_position = margin * frame_line_height;
 20254             }
 20255           else
 20256             centering_position = margin * frame_line_height + pt_offset;
 20257         }
 20258       else
 20259         /* Set the window start half the height of the window backward
 20260            from point.  */
 20261         centering_position = window_box_height (w) / 2;
 20262     }
 20263   if (current_buffer->long_line_optimizations_p
 20264       && it.line_wrap == TRUNCATE)
 20265     {
 20266       /* For very long and truncated lines, go back using a simplified
 20267          method, which ignored any inaccuracies due to line-height
 20268          differences, display properties/overlays, etc.  */
 20269       int nlines = centering_position / frame_line_height;
 20270 
 20271       while (nlines-- && IT_CHARPOS (it) > BEGV)
 20272         back_to_previous_visible_line_start (&it);
 20273       reseat_1 (&it, it.current.pos, true);
 20274     }
 20275   else
 20276     move_it_vertically_backward (&it, centering_position);
 20277 
 20278   eassert (IT_CHARPOS (it) >= BEGV);
 20279 
 20280   /* The function move_it_vertically_backward may move over more
 20281      than the specified y-distance.  If it->w is small, e.g. a
 20282      mini-buffer window, we may end up in front of the window's
 20283      display area.  Start displaying at the start of the line
 20284      containing PT in this case.  */
 20285   if (it.current_y <= 0)
 20286     {
 20287       init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20288       move_it_vertically_backward (&it, 0);
 20289       it.current_y = 0;
 20290     }
 20291 
 20292   it.current_x = it.hpos = 0;
 20293 
 20294   /* Set the window start position here explicitly, to avoid an
 20295      infinite loop in case the functions in window-scroll-functions
 20296      get errors.  */
 20297   set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
 20298 
 20299   /* Run scroll hooks.  */
 20300   startp = run_window_scroll_functions (window, it.current.pos);
 20301 
 20302   /* We invoke try_window and try_window_reusing_current_matrix below,
 20303      and they manipulate the bidi cache.  Save and restore the cache
 20304      state of our iterator, so we could continue using it after that.  */
 20305   itdata = bidi_shelve_cache ();
 20306 
 20307   /* Redisplay the window.  */
 20308   use_desired_matrix = false;
 20309   if (!current_matrix_up_to_date_p
 20310       || windows_or_buffers_changed
 20311       || f->cursor_type_changed
 20312       /* Don't use try_window_reusing_current_matrix in this case
 20313          because it can have changed the buffer.  */
 20314       || !NILP (Vwindow_scroll_functions)
 20315       || !just_this_one_p
 20316       || MINI_WINDOW_P (w)
 20317       || !(used_current_matrix_p
 20318            = try_window_reusing_current_matrix (w)))
 20319     use_desired_matrix = (try_window (window, startp, 0) == 1);
 20320 
 20321   bidi_unshelve_cache (itdata, false);
 20322 
 20323   /* If new fonts have been loaded (due to fontsets), give up.  We
 20324      have to start a new redisplay since we need to re-adjust glyph
 20325      matrices.  */
 20326   if (f->fonts_changed)
 20327     goto need_larger_matrices;
 20328 
 20329   /* If cursor did not appear assume that the middle of the window is
 20330      in the first line of the window.  Do it again with the next line.
 20331      (Imagine a window of height 100, displaying two lines of height
 20332      60.  Moving back 50 from it->last_visible_y will end in the first
 20333      line.)  */
 20334   if (w->cursor.vpos < 0)
 20335     {
 20336       if (w->window_end_valid && PT >= Z - w->window_end_pos)
 20337         {
 20338           clear_glyph_matrix (w->desired_matrix);
 20339           move_it_by_lines (&it, 1);
 20340           try_window (window, it.current.pos, 0);
 20341         }
 20342       else if (PT < IT_CHARPOS (it))
 20343         {
 20344           clear_glyph_matrix (w->desired_matrix);
 20345           move_it_by_lines (&it, -1);
 20346           try_window (window, it.current.pos, 0);
 20347         }
 20348       else if (scroll_conservatively > SCROLL_LIMIT
 20349                && (it.method == GET_FROM_STRING
 20350                    || overlay_touches_p (IT_CHARPOS (it)))
 20351                && IT_CHARPOS (it) < ZV)
 20352         {
 20353           /* If the window starts with a before-string that spans more
 20354              than one screen line, using that position to display the
 20355              window might fail to bring point into the view, because
 20356              start_display will always start by displaying the string,
 20357              whereas the code above determines where to set w->start
 20358              by the buffer position of the place where it takes screen
 20359              coordinates.  Try to recover by finding the next screen
 20360              line that displays buffer text.  */
 20361           ptrdiff_t pos0 = IT_CHARPOS (it);
 20362 
 20363           clear_glyph_matrix (w->desired_matrix);
 20364           do {
 20365             move_it_by_lines (&it, 1);
 20366           } while (IT_CHARPOS (it) == pos0);
 20367           try_window (window, it.current.pos, 0);
 20368         }
 20369       else
 20370         {
 20371           /* Not much we can do about it.  */
 20372         }
 20373     }
 20374 
 20375   /* Consider the following case: Window starts at BEGV, there is
 20376      invisible, intangible text at BEGV, so that display starts at
 20377      some point START > BEGV.  It can happen that we are called with
 20378      PT somewhere between BEGV and START.  Try to handle that case,
 20379      and similar ones.  */
 20380   if (w->cursor.vpos < 0)
 20381     {
 20382       /* Prefer the desired matrix to the current matrix, if possible,
 20383          in the fallback calculations below.  This is because using
 20384          the current matrix might completely goof, e.g. if its first
 20385          row is after point.  */
 20386       struct glyph_matrix *matrix =
 20387         use_desired_matrix ? w->desired_matrix : w->current_matrix;
 20388       /* First, try locating the proper glyph row for PT.  */
 20389       struct glyph_row *row =
 20390         row_containing_pos (w, PT, matrix->rows, NULL, 0);
 20391 
 20392       /* Sometimes point is at the beginning of invisible text that is
 20393          before the 1st character displayed in the row.  In that case,
 20394          row_containing_pos fails to find the row, because no glyphs
 20395          with appropriate buffer positions are present in the row.
 20396          Therefore, we next try to find the row which shows the 1st
 20397          position after the invisible text.  */
 20398       if (!row)
 20399         {
 20400           Lisp_Object val =
 20401             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20402                                            Qnil, NULL);
 20403 
 20404           if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 20405             {
 20406               ptrdiff_t alt_pos;
 20407               Lisp_Object invis_end =
 20408                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20409                                                    Qnil, Qnil);
 20410 
 20411               if (FIXNATP (invis_end))
 20412                 alt_pos = XFIXNAT (invis_end);
 20413               else
 20414                 alt_pos = ZV;
 20415               row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
 20416             }
 20417         }
 20418       /* Finally, fall back on the first row of the window after the
 20419          tab-line and header line (if any).  This is slightly better
 20420          than not displaying the cursor at all.  */
 20421       if (!row)
 20422         {
 20423           row = matrix->rows;
 20424           /* Skip the tab-line and header-line rows, if any.  */
 20425           if (row->tab_line_p)
 20426             ++row;
 20427           if (row->mode_line_p)
 20428             ++row;
 20429         }
 20430       set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
 20431     }
 20432 
 20433   if (!cursor_row_fully_visible_p (w, false, false, false))
 20434     {
 20435       /* If vscroll is enabled, disable it and try again.  */
 20436       if (w->vscroll)
 20437         {
 20438           w->vscroll = 0;
 20439           clear_glyph_matrix (w->desired_matrix);
 20440           goto recenter;
 20441         }
 20442 
 20443       /* Users who set scroll-conservatively to a large number want
 20444          point just above/below the scroll margin.  If we ended up
 20445          with point's row partially visible, move the window start to
 20446          make that row fully visible and out of the margin.  */
 20447       if (scroll_conservatively > SCROLL_LIMIT)
 20448         {
 20449           int window_total_lines
 20450             = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
 20451           bool move_down = w->cursor.vpos >= window_total_lines / 2;
 20452 
 20453           move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
 20454           clear_glyph_matrix (w->desired_matrix);
 20455           if (1 == try_window (window, it.current.pos,
 20456                                TRY_WINDOW_CHECK_MARGINS))
 20457             goto done;
 20458         }
 20459 
 20460       /* If centering point failed to make the whole line visible,
 20461          put point at the top instead.  That has to make the whole line
 20462          visible, if it can be done.  */
 20463       if (centering_position == 0)
 20464         goto done;
 20465 
 20466       clear_glyph_matrix (w->desired_matrix);
 20467       centering_position = 0;
 20468       goto recenter;
 20469     }
 20470 
 20471  done:
 20472 
 20473   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20474   w->start_at_line_beg = (CHARPOS (startp) == BEGV
 20475                           || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
 20476 
 20477   /* Display the mode line, header line, and tab-line, if we must.  */
 20478   if ((update_mode_line
 20479        /* If window not full width, must redo its mode line
 20480           if (a) the window to its side is being redone and
 20481           (b) we do a frame-based redisplay.  This is a consequence
 20482           of how inverted lines are drawn in frame-based redisplay.  */
 20483        || (!just_this_one_p
 20484            && !FRAME_WINDOW_P (f)
 20485            && !WINDOW_FULL_WIDTH_P (w))
 20486        /* Line number to display.  */
 20487        || w->base_line_pos > 0
 20488        /* Column number is displayed and different from the one displayed.  */
 20489        || (w->column_number_displayed != -1
 20490            && (w->column_number_displayed != current_column ())))
 20491       /* This means that the window has a mode line.  */
 20492       && (window_wants_mode_line (w)
 20493           || window_wants_header_line (w)
 20494           || window_wants_tab_line (w)))
 20495     {
 20496       specpdl_ref count1 = SPECPDL_INDEX ();
 20497 
 20498       specbind (Qinhibit_quit, Qt);
 20499       display_mode_lines (w);
 20500       unbind_to (count1, Qnil);
 20501 
 20502       /* If mode line height has changed, arrange for a thorough
 20503          immediate redisplay using the correct mode line height.  */
 20504       if (window_wants_mode_line (w)
 20505           && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
 20506         {
 20507           f->fonts_changed = true;
 20508           w->mode_line_height = -1;
 20509           MATRIX_MODE_LINE_ROW (w->current_matrix)->height
 20510             = DESIRED_MODE_LINE_HEIGHT (w);
 20511         }
 20512 
 20513       /* If tab line height has changed, arrange for a thorough
 20514          immediate redisplay using the correct tab line height.  */
 20515       if (window_wants_tab_line (w)
 20516           && CURRENT_TAB_LINE_HEIGHT (w) != DESIRED_TAB_LINE_HEIGHT (w))
 20517         {
 20518           f->fonts_changed = true;
 20519           w->tab_line_height = -1;
 20520           MATRIX_TAB_LINE_ROW (w->current_matrix)->height
 20521             = DESIRED_TAB_LINE_HEIGHT (w);
 20522         }
 20523 
 20524       /* If header line height has changed, arrange for a thorough
 20525          immediate redisplay using the correct header line height.  */
 20526       if (window_wants_header_line (w)
 20527           && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
 20528         {
 20529           f->fonts_changed = true;
 20530           w->header_line_height = -1;
 20531           MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
 20532             = DESIRED_HEADER_LINE_HEIGHT (w);
 20533         }
 20534 
 20535       if (f->fonts_changed)
 20536         goto need_larger_matrices;
 20537     }
 20538 
 20539   if (!line_number_displayed && w->base_line_pos != -1)
 20540     {
 20541       w->base_line_pos = 0;
 20542       w->base_line_number = 0;
 20543     }
 20544 
 20545  finish_menu_bars:
 20546 
 20547   /* When we reach a frame's selected window, redo the frame's menu
 20548      bar, tool bar, tab-bar, and the frame's title.  */
 20549   if (update_mode_line
 20550       && EQ (FRAME_SELECTED_WINDOW (f), window))
 20551     {
 20552       bool redisplay_menu_p;
 20553 
 20554       if (FRAME_WINDOW_P (f))
 20555         {
 20556 #ifdef HAVE_EXT_MENU_BAR
 20557           redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
 20558 #else
 20559           redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20560 #endif
 20561         }
 20562       else
 20563         redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20564 
 20565       if (redisplay_menu_p)
 20566         display_menu_bar (w);
 20567 
 20568 #ifdef HAVE_WINDOW_SYSTEM
 20569       if (FRAME_WINDOW_P (f))
 20570         {
 20571           if (WINDOWP (f->tab_bar_window)
 20572               && (FRAME_TAB_BAR_LINES (f) > 0
 20573                   || !NILP (Vauto_resize_tab_bars))
 20574               && redisplay_tab_bar (f))
 20575             ignore_mouse_drag_p = true;
 20576 
 20577 #ifdef HAVE_EXT_TOOL_BAR
 20578           if (FRAME_EXTERNAL_TOOL_BAR (f))
 20579             update_frame_tool_bar (f);
 20580 #else
 20581           if (WINDOWP (f->tool_bar_window)
 20582               && (FRAME_TOOL_BAR_LINES (f) > 0
 20583                   || !NILP (Vauto_resize_tool_bars))
 20584               && redisplay_tool_bar (f))
 20585             ignore_mouse_drag_p = true;
 20586 #endif
 20587         }
 20588       else
 20589         {
 20590           if ((FRAME_TAB_BAR_LINES (f) > 0))
 20591             display_tab_bar (w);
 20592         }
 20593 
 20594       gui_consider_frame_title (w->frame);
 20595 #else
 20596       if ((FRAME_TAB_BAR_LINES (f) > 0))
 20597         display_tab_bar (w);
 20598 #endif
 20599     }
 20600 
 20601 #ifdef HAVE_WINDOW_SYSTEM
 20602   if (FRAME_WINDOW_P (f)
 20603       && update_window_fringes (w, (just_this_one_p
 20604                                     || (!used_current_matrix_p && !overlay_arrow_seen)
 20605                                     || w->pseudo_window_p)))
 20606     {
 20607       update_begin (f);
 20608       block_input ();
 20609       if (draw_window_fringes (w, true))
 20610         {
 20611           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 20612             gui_draw_right_divider (w);
 20613           else
 20614             gui_draw_vertical_border (w);
 20615         }
 20616       unblock_input ();
 20617       update_end (f);
 20618     }
 20619 
 20620   if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 20621     gui_draw_bottom_divider (w);
 20622 #endif /* HAVE_WINDOW_SYSTEM */
 20623 
 20624   /* We go to this label, with fonts_changed set, if it is
 20625      necessary to try again using larger glyph matrices.
 20626      We have to redeem the scroll bar even in this case,
 20627      because the loop in redisplay_internal expects that.  */
 20628  need_larger_matrices:
 20629   ;
 20630  finish_scroll_bars:
 20631 
 20632    if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20633     {
 20634       if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
 20635         /* Set the thumb's position and size.  */
 20636         set_vertical_scroll_bar (w);
 20637 
 20638       if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20639         /* Set the thumb's position and size.  */
 20640         set_horizontal_scroll_bar (w);
 20641 
 20642       /* Note that we actually used the scroll bar attached to this
 20643          window, so it shouldn't be deleted at the end of redisplay.  */
 20644       if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
 20645         (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
 20646     }
 20647 
 20648   /* Restore current_buffer and value of point in it.  The window
 20649      update may have changed the buffer, so first make sure `opoint'
 20650      is still valid (Bug#6177).  */
 20651   if (CHARPOS (opoint) < BEGV)
 20652     TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
 20653   else if (CHARPOS (opoint) > ZV)
 20654     TEMP_SET_PT_BOTH (Z, Z_BYTE);
 20655   else
 20656     TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
 20657 
 20658   set_buffer_internal_1 (old);
 20659   /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
 20660      shorter.  This can be caused by log truncation in *Messages*.  */
 20661   if (CHARPOS (lpoint) <= ZV)
 20662     TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 20663 
 20664   unbind_to (count, Qnil);
 20665 }
 20666 
 20667 
 20668 /* Build the complete desired matrix of WINDOW with a window start
 20669    buffer position POS.
 20670 
 20671    Value is 1 if successful.  It is zero if fonts were loaded during
 20672    redisplay or the dimensions of the desired matrix were found
 20673    insufficient, which makes re-adjusting glyph matrices necessary.
 20674    Value is -1 if point would appear in the scroll margins.  (We check
 20675    the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is unset in
 20676    FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is set in
 20677    FLAGS.)
 20678 
 20679    Note that 'x-show-tip' invokes this function in a special way, and
 20680    in that case the return value of zero doesn't necessarily mean the
 20681    glyph matrices need to be re-adjusted, if the entire text of the
 20682    tooltip was processed and has its glyphs in the matrix's glyph
 20683    rows, i.e. if the dimensions of the matrix were found insufficient
 20684    while producing empty glyph rows beyond ZV.  */
 20685 
 20686 int
 20687 try_window (Lisp_Object window, struct text_pos pos, int flags)
 20688 {
 20689   struct window *w = XWINDOW (window);
 20690   struct it it;
 20691   struct glyph_row *last_text_row = NULL;
 20692   struct frame *f = XFRAME (w->frame);
 20693   int cursor_vpos = w->cursor.vpos;
 20694 
 20695   /* Make POS the new window start.  */
 20696   set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
 20697 
 20698   /* Mark cursor position as unknown.  No overlay arrow seen.  */
 20699   w->cursor.vpos = -1;
 20700   overlay_arrow_seen = false;
 20701 
 20702   /* Initialize iterator and info to start at POS.  */
 20703   start_display (&it, w, pos);
 20704   it.glyph_row->reversed_p = false;
 20705 
 20706   /* Display all lines of W.  */
 20707   while (it.current_y < it.last_visible_y)
 20708     {
 20709       int last_row_scale = it.w->nrows_scale_factor;
 20710       int last_col_scale = it.w->ncols_scale_factor;
 20711       if (display_line (&it, cursor_vpos))
 20712         last_text_row = it.glyph_row - 1;
 20713       if (f->fonts_changed
 20714           && !((flags & TRY_WINDOW_IGNORE_FONTS_CHANGE)
 20715                /* If the matrix dimensions are insufficient, we _must_
 20716                   fail and let dispnew.c reallocate the matrix.  */
 20717                && last_row_scale == it.w->nrows_scale_factor
 20718                && last_col_scale == it.w->ncols_scale_factor))
 20719         return 0;
 20720     }
 20721 
 20722   /* Save the character position of 'it' before we call
 20723      'start_display' again.  */
 20724   ptrdiff_t it_charpos = IT_CHARPOS (it);
 20725 
 20726   /* Don't let the cursor end in the scroll margins.  However, when
 20727      the window is vscrolled, we leave it to vscroll to handle the
 20728      margins, see window_scroll_pixel_based.  */
 20729   if ((flags & TRY_WINDOW_CHECK_MARGINS)
 20730       && w->vscroll == 0
 20731       && !MINI_WINDOW_P (w))
 20732     {
 20733       int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 20734       int bot_scroll_margin = top_scroll_margin;
 20735       if (window_wants_header_line (w))
 20736         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 20737       if (window_wants_tab_line (w))
 20738         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 20739       start_display (&it, w, pos);
 20740 
 20741       if ((w->cursor.y >= 0
 20742            && w->cursor.y < top_scroll_margin
 20743            && CHARPOS (pos) > BEGV)
 20744           /* rms: considering make_cursor_line_fully_visible_p here
 20745              seems to give wrong results.  We don't want to recenter
 20746              when the last line is partly visible, we want to allow
 20747              that case to be handled in the usual way.  */
 20748           || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
 20749                             - bot_scroll_margin - 1))
 20750         {
 20751           w->cursor.vpos = -1;
 20752           clear_glyph_matrix (w->desired_matrix);
 20753           return -1;
 20754         }
 20755     }
 20756 
 20757   /* If bottom moved off end of frame, change mode line percentage.  */
 20758   if (w->window_end_pos <= 0 && Z != it_charpos)
 20759     w->update_mode_line = true;
 20760 
 20761   /* Set window_end_pos to the offset of the last character displayed
 20762      on the window from the end of current_buffer.  Set
 20763      window_end_vpos to its row number.  */
 20764   if (last_text_row)
 20765     {
 20766       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
 20767       adjust_window_ends (w, last_text_row, false);
 20768       eassert
 20769         (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
 20770                                                  w->window_end_vpos)));
 20771     }
 20772   else
 20773     {
 20774       w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 20775       w->window_end_pos = Z - ZV;
 20776       w->window_end_vpos = 0;
 20777     }
 20778 
 20779   /* But that is not valid info until redisplay finishes.  */
 20780   w->window_end_valid = false;
 20781   return 1;
 20782 }
 20783 
 20784 
 20785 
 20786 /************************************************************************
 20787     Window redisplay reusing current matrix when buffer has not changed
 20788  ************************************************************************/
 20789 
 20790 /* Try redisplay of window W showing an unchanged buffer with a
 20791    different window start than the last time it was displayed by
 20792    reusing its current matrix.  Value is true if successful.
 20793    W->start is the new window start.  */
 20794 
 20795 static bool
 20796 try_window_reusing_current_matrix (struct window *w)
 20797 {
 20798   struct frame *f = XFRAME (w->frame);
 20799   struct glyph_row *bottom_row;
 20800   struct it it;
 20801   struct run run;
 20802   struct text_pos start, new_start;
 20803   int nrows_scrolled, i;
 20804   struct glyph_row *last_text_row;
 20805   struct glyph_row *last_reused_text_row;
 20806   struct glyph_row *start_row;
 20807   int start_vpos, min_y, max_y;
 20808 
 20809 #ifdef GLYPH_DEBUG
 20810   if (inhibit_try_window_reusing)
 20811     return false;
 20812 #endif
 20813 
 20814   if (/* This function doesn't handle terminal frames.  */
 20815       !FRAME_WINDOW_P (f)
 20816       /* Don't try to reuse the display if windows have been split
 20817          or such.  */
 20818       || windows_or_buffers_changed
 20819       || f->cursor_type_changed
 20820       /* This function cannot handle buffers where the overlay arrow
 20821          is shown on the fringes, because if the arrow position
 20822          changes, we cannot just reuse the current matrix.  */
 20823       || overlay_arrow_in_current_buffer_p ())
 20824     return false;
 20825 
 20826   /* Can't do this if showing trailing whitespace.  */
 20827   if (!NILP (Vshow_trailing_whitespace))
 20828     return false;
 20829 
 20830   /* If top-line visibility has changed, give up.  */
 20831   if (window_wants_tab_line (w)
 20832       != MATRIX_TAB_LINE_ROW (w->current_matrix)->mode_line_p)
 20833     return false;
 20834 
 20835   /* If top-line visibility has changed, give up.  */
 20836   if (window_wants_header_line (w)
 20837       != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
 20838     return false;
 20839 
 20840   /* Give up if old or new display is scrolled vertically.  We could
 20841      make this function handle this, but right now it doesn't.  */
 20842   start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 20843   if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
 20844     return false;
 20845 
 20846   /* Clear the desired matrix for the display below.  */
 20847   clear_glyph_matrix (w->desired_matrix);
 20848 
 20849   /* Give up if line numbers are being displayed, because reusing the
 20850      current matrix might use the wrong width for line-number
 20851      display.  */
 20852   if (!NILP (Vdisplay_line_numbers))
 20853     return false;
 20854 
 20855   /* Can't scroll the display of w32 GUI frames when position of point
 20856      is indicated by the system caret, because scrolling the display
 20857      will then "copy" the pixels used by the caret.  */
 20858 #ifdef HAVE_NTGUI
 20859   if (w32_use_visible_system_caret)
 20860     return false;
 20861 #endif
 20862 
 20863   /* The variable new_start now holds the new window start.  The old
 20864      start `start' can be determined from the current matrix.  */
 20865   SET_TEXT_POS_FROM_MARKER (new_start, w->start);
 20866   start = start_row->minpos;
 20867   start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 20868 
 20869   if (CHARPOS (new_start) <= CHARPOS (start))
 20870     {
 20871       /* Don't use this method if the display starts with an ellipsis
 20872          displayed for invisible text.  It's not easy to handle that case
 20873          below, and it's certainly not worth the effort since this is
 20874          not a frequent case.  */
 20875       if (in_ellipses_for_invisible_text_p (&start_row->start, w))
 20876         return false;
 20877 
 20878       IF_DEBUG (debug_method_add (w, "twu1"));
 20879 
 20880       /* Display up to a row that can be reused.  The variable
 20881          last_text_row is set to the last row displayed that displays
 20882          text.  Note that it.vpos == 0 if or if not there is a
 20883          header-line; it's not the same as the MATRIX_ROW_VPOS!  */
 20884       start_display (&it, w, new_start);
 20885       w->cursor.vpos = -1;
 20886       last_text_row = last_reused_text_row = NULL;
 20887 
 20888       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 20889         {
 20890           /* If we have reached into the characters in the START row,
 20891              that means the line boundaries have changed.  So we
 20892              can't start copying with the row START.  Maybe it will
 20893              work to start copying with the following row.  */
 20894           while (IT_CHARPOS (it) > CHARPOS (start))
 20895             {
 20896               /* Advance to the next row as the "start".  */
 20897               start_row++;
 20898               start = start_row->minpos;
 20899               /* If there are no more rows to try, or just one, give up.  */
 20900               if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
 20901                   || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
 20902                   || CHARPOS (start) == ZV)
 20903                 {
 20904                   clear_glyph_matrix (w->desired_matrix);
 20905                   return false;
 20906                 }
 20907 
 20908               start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 20909             }
 20910           /* If we have reached alignment, we can copy the rest of the
 20911              rows.  */
 20912           if (IT_CHARPOS (it) == CHARPOS (start)
 20913               /* Don't accept "alignment" inside a display vector,
 20914                  since start_row could have started in the middle of
 20915                  that same display vector (thus their character
 20916                  positions match), and we have no way of telling if
 20917                  that is the case.  */
 20918               && it.current.dpvec_index < 0)
 20919             break;
 20920 
 20921           it.glyph_row->reversed_p = false;
 20922           if (display_line (&it, -1))
 20923             last_text_row = it.glyph_row - 1;
 20924 
 20925         }
 20926 
 20927       /* A value of current_y < last_visible_y means that we stopped
 20928          at the previous window start, which in turn means that we
 20929          have at least one reusable row.  */
 20930       if (it.current_y < it.last_visible_y)
 20931         {
 20932           struct glyph_row *row;
 20933 
 20934           /* IT.vpos always starts from 0; it counts text lines.  */
 20935           nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
 20936 
 20937           /* Find PT if not already found in the lines displayed.  */
 20938           if (w->cursor.vpos < 0)
 20939             {
 20940               int dy = it.current_y - start_row->y;
 20941 
 20942               row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 20943               row = row_containing_pos (w, PT, row, NULL, dy);
 20944               if (row)
 20945                 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
 20946                                      dy, nrows_scrolled);
 20947               else
 20948                 {
 20949                   clear_glyph_matrix (w->desired_matrix);
 20950                   return false;
 20951                 }
 20952             }
 20953 
 20954           /* Scroll the display.  Do it before the current matrix is
 20955              changed.  The problem here is that update has not yet
 20956              run, i.e. part of the current matrix is not up to date.
 20957              scroll_run_hook will clear the cursor, and use the
 20958              current matrix to get the height of the row the cursor is
 20959              in.  */
 20960           run.current_y = start_row->y;
 20961           run.desired_y = it.current_y;
 20962           run.height = it.last_visible_y - it.current_y;
 20963 
 20964           if (run.height > 0 && run.current_y != run.desired_y)
 20965             {
 20966 #ifdef HAVE_WINDOW_SYSTEM
 20967               update_begin (f);
 20968               gui_update_window_begin (w);
 20969               FRAME_RIF (f)->clear_window_mouse_face (w);
 20970               FRAME_RIF (f)->scroll_run_hook (w, &run);
 20971               gui_update_window_end (w, false, false);
 20972               update_end (f);
 20973 #endif
 20974             }
 20975 
 20976           /* Shift current matrix down by nrows_scrolled lines.  */
 20977           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 20978           rotate_matrix (w->current_matrix,
 20979                          start_vpos,
 20980                          MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 20981                          nrows_scrolled);
 20982 
 20983           /* Disable lines that must be updated.  */
 20984           for (i = 0; i < nrows_scrolled; ++i)
 20985             (start_row + i)->enabled_p = false;
 20986 
 20987           /* Re-compute Y positions.  */
 20988           min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 20989           max_y = it.last_visible_y;
 20990           for (row = start_row + nrows_scrolled;
 20991                row < bottom_row;
 20992                ++row)
 20993             {
 20994               row->y = it.current_y;
 20995               row->visible_height = row->height;
 20996 
 20997               if (row->y < min_y)
 20998                 row->visible_height -= min_y - row->y;
 20999               if (row->y + row->height > max_y)
 21000                 row->visible_height -= row->y + row->height - max_y;
 21001               if (row->fringe_bitmap_periodic_p)
 21002                 row->redraw_fringe_bitmaps_p = true;
 21003 
 21004               it.current_y += row->height;
 21005 
 21006               if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21007                 last_reused_text_row = row;
 21008               if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
 21009                 break;
 21010             }
 21011 
 21012           /* Disable lines in the current matrix which are now
 21013              below the window.  */
 21014           for (++row; row < bottom_row; ++row)
 21015             row->enabled_p = row->mode_line_p = row->tab_line_p = false;
 21016         }
 21017 
 21018       /* Update window_end_pos etc.; last_reused_text_row is the last
 21019          reused row from the current matrix containing text, if any.
 21020          The value of last_text_row is the last displayed line
 21021          containing text.  */
 21022       if (last_reused_text_row)
 21023         adjust_window_ends (w, last_reused_text_row, true);
 21024       else if (last_text_row)
 21025         adjust_window_ends (w, last_text_row, false);
 21026       else
 21027         {
 21028           /* This window must be completely empty.  */
 21029           w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21030           w->window_end_pos = Z - ZV;
 21031           w->window_end_vpos = 0;
 21032         }
 21033       w->window_end_valid = false;
 21034 
 21035       /* Update hint: don't try scrolling again in update_window.  */
 21036       w->desired_matrix->no_scrolling_p = true;
 21037 
 21038 #ifdef GLYPH_DEBUG
 21039       debug_method_add (w, "try_window_reusing_current_matrix 1");
 21040 #endif
 21041       return true;
 21042     }
 21043   else if (CHARPOS (new_start) > CHARPOS (start))
 21044     {
 21045       struct glyph_row *pt_row, *row;
 21046       struct glyph_row *first_reusable_row;
 21047       struct glyph_row *first_row_to_display;
 21048       int dy;
 21049       int yb = window_text_bottom_y (w);
 21050 
 21051       /* Find the row starting at new_start, if there is one.  Don't
 21052          reuse a partially visible line at the end.  */
 21053       first_reusable_row = start_row;
 21054       while (first_reusable_row->enabled_p
 21055              && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
 21056              && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21057                  < CHARPOS (new_start)))
 21058         ++first_reusable_row;
 21059 
 21060       /* Give up if there is no row to reuse.  */
 21061       if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
 21062           || !first_reusable_row->enabled_p
 21063           || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21064               != CHARPOS (new_start)))
 21065         return false;
 21066 
 21067       /* We can reuse fully visible rows beginning with
 21068          first_reusable_row to the end of the window.  Set
 21069          first_row_to_display to the first row that cannot be reused.
 21070          Set pt_row to the row containing point, if there is any.  */
 21071       pt_row = NULL;
 21072       for (first_row_to_display = first_reusable_row;
 21073            MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
 21074            ++first_row_to_display)
 21075         {
 21076           if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
 21077               && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21078                   || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21079                       && first_row_to_display->ends_at_zv_p
 21080                       && pt_row == NULL)))
 21081             pt_row = first_row_to_display;
 21082         }
 21083 
 21084       if (first_row_to_display->y >= yb)
 21085         return false;
 21086 
 21087       /* Start displaying at the start of first_row_to_display.  */
 21088       init_to_row_start (&it, w, first_row_to_display);
 21089 
 21090       nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
 21091                         - start_vpos);
 21092       it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
 21093                  - nrows_scrolled);
 21094       it.current_y = (first_row_to_display->y - first_reusable_row->y
 21095                       + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 21096 
 21097       /* Display lines beginning with first_row_to_display in the
 21098          desired matrix.  Set last_text_row to the last row displayed
 21099          that displays text.  */
 21100       it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
 21101       if (pt_row == NULL)
 21102         w->cursor.vpos = -1;
 21103       last_text_row = NULL;
 21104       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21105         if (display_line (&it, w->cursor.vpos))
 21106           last_text_row = it.glyph_row - 1;
 21107 
 21108       /* If point is in a reused row, adjust y and vpos of the cursor
 21109          position.  */
 21110       if (pt_row)
 21111         {
 21112           w->cursor.vpos -= nrows_scrolled;
 21113           w->cursor.y -= first_reusable_row->y - start_row->y;
 21114         }
 21115 
 21116       /* Give up if point isn't in a row displayed or reused.  (This
 21117          also handles the case where w->cursor.vpos < nrows_scrolled
 21118          after the calls to display_line, which can happen with scroll
 21119          margins.  See bug#1295.)  */
 21120       if (w->cursor.vpos < 0)
 21121         {
 21122           clear_glyph_matrix (w->desired_matrix);
 21123           return false;
 21124         }
 21125 
 21126       /* Scroll the display.  */
 21127       run.current_y = first_reusable_row->y;
 21128       run.desired_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21129       run.height = it.last_visible_y - run.current_y;
 21130       dy = run.current_y - run.desired_y;
 21131 
 21132       if (run.height)
 21133         {
 21134 #ifdef HAVE_WINDOW_SYSTEM
 21135           update_begin (f);
 21136           gui_update_window_begin (w);
 21137           FRAME_RIF (f)->clear_window_mouse_face (w);
 21138           FRAME_RIF (f)->scroll_run_hook (w, &run);
 21139           gui_update_window_end (w, false, false);
 21140           update_end (f);
 21141 #endif
 21142         }
 21143 
 21144       /* Adjust Y positions of reused rows.  */
 21145       bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21146       min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21147       max_y = it.last_visible_y;
 21148       for (row = first_reusable_row; row < first_row_to_display; ++row)
 21149         {
 21150           row->y -= dy;
 21151           row->visible_height = row->height;
 21152           if (row->y < min_y)
 21153             row->visible_height -= min_y - row->y;
 21154           if (row->y + row->height > max_y)
 21155             row->visible_height -= row->y + row->height - max_y;
 21156           if (row->fringe_bitmap_periodic_p)
 21157             row->redraw_fringe_bitmaps_p = true;
 21158         }
 21159 
 21160       /* Scroll the current matrix.  */
 21161       eassert (nrows_scrolled > 0);
 21162       rotate_matrix (w->current_matrix,
 21163                      start_vpos,
 21164                      MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21165                      -nrows_scrolled);
 21166 
 21167       /* Disable rows not reused.  */
 21168       for (row -= nrows_scrolled; row < bottom_row; ++row)
 21169         row->enabled_p = false;
 21170 
 21171       /* Point may have moved to a different line, so we cannot assume that
 21172          the previous cursor position is valid; locate the correct row.  */
 21173       if (pt_row)
 21174         {
 21175           for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 21176                row < bottom_row
 21177                  && PT >= MATRIX_ROW_END_CHARPOS (row)
 21178                  && !row->ends_at_zv_p;
 21179                row++)
 21180             {
 21181               w->cursor.vpos++;
 21182               w->cursor.y = row->y;
 21183             }
 21184           if (row < bottom_row)
 21185             {
 21186               /* Can't simply scan the row for point with
 21187                  bidi-reordered glyph rows.  Let set_cursor_from_row
 21188                  figure out where to put the cursor, and if it fails,
 21189                  give up.  */
 21190               if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 21191                 {
 21192                   if (!set_cursor_from_row (w, row, w->current_matrix,
 21193                                             0, 0, 0, 0))
 21194                     {
 21195                       clear_glyph_matrix (w->desired_matrix);
 21196                       return false;
 21197                     }
 21198                 }
 21199               else
 21200                 {
 21201                   struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
 21202                   struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21203 
 21204                   for (; glyph < end
 21205                          && (!BUFFERP (glyph->object)
 21206                              || glyph->charpos < PT);
 21207                        glyph++)
 21208                     {
 21209                       w->cursor.hpos++;
 21210                       w->cursor.x += glyph->pixel_width;
 21211                     }
 21212                 }
 21213             }
 21214         }
 21215 
 21216       /* Adjust window end.  A null value of last_text_row means that
 21217          the window end is in reused rows which in turn means that
 21218          only its vpos can have changed.  */
 21219       if (last_text_row)
 21220         adjust_window_ends (w, last_text_row, false);
 21221       else
 21222         w->window_end_vpos -= nrows_scrolled;
 21223 
 21224       w->window_end_valid = false;
 21225       w->desired_matrix->no_scrolling_p = true;
 21226 
 21227 #ifdef GLYPH_DEBUG
 21228       debug_method_add (w, "try_window_reusing_current_matrix 2");
 21229 #endif
 21230       return true;
 21231     }
 21232 
 21233   return false;
 21234 }
 21235 
 21236 
 21237 
 21238 /************************************************************************
 21239    Window redisplay reusing current matrix when buffer has changed
 21240  ************************************************************************/
 21241 
 21242 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
 21243 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
 21244                                                           ptrdiff_t *, ptrdiff_t *);
 21245 static struct glyph_row *
 21246 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
 21247                                struct glyph_row *);
 21248 
 21249 
 21250 /* Return the last row in MATRIX displaying text.  If row START is
 21251    non-null, start searching with that row.  IT gives the dimensions
 21252    of the display.  Value is null if matrix is empty; otherwise it is
 21253    a pointer to the row found.  */
 21254 
 21255 static struct glyph_row *
 21256 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
 21257                                struct glyph_row *start)
 21258 {
 21259   struct glyph_row *row, *row_found;
 21260 
 21261   /* Set row_found to the last row in IT->w's current matrix
 21262      displaying text.  The loop looks funny but think of partially
 21263      visible lines.  */
 21264   row_found = NULL;
 21265   row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
 21266   while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21267     {
 21268       eassert (row->enabled_p);
 21269       row_found = row;
 21270       if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
 21271         break;
 21272       ++row;
 21273     }
 21274 
 21275   return row_found;
 21276 }
 21277 
 21278 
 21279 /* Return the last row in the current matrix of W that is not affected
 21280    by changes at the start of current_buffer that occurred since W's
 21281    current matrix was built.  Value is null if no such row exists.
 21282 
 21283    BEG_UNCHANGED is the number of characters unchanged at the start of
 21284    current_buffer.  BEG + BEG_UNCHANGED is the buffer position of the
 21285    first changed character in current_buffer.  Characters at positions <
 21286    BEG + BEG_UNCHANGED are at the same buffer positions as they were
 21287    when the current matrix was built.  */
 21288 
 21289 static struct glyph_row *
 21290 find_last_unchanged_at_beg_row (struct window *w)
 21291 {
 21292   ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
 21293   struct glyph_row *row;
 21294   struct glyph_row *row_found = NULL;
 21295   int yb = window_text_bottom_y (w);
 21296 
 21297   /* Find the last row displaying unchanged text.  */
 21298   for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21299        MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21300          && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
 21301        ++row)
 21302     {
 21303       if (/* If row ends before first_changed_pos, it is unchanged,
 21304              except in some case.  */
 21305           MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
 21306           /* When row ends in ZV and we write at ZV it is not
 21307              unchanged.  */
 21308           && !row->ends_at_zv_p
 21309           /* When first_changed_pos is the end of a continued line,
 21310              row is not unchanged because it may be no longer
 21311              continued.  */
 21312           && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
 21313                && (row->continued_p
 21314                    || row->exact_window_width_line_p))
 21315           /* If ROW->end is beyond ZV, then ROW->end is outdated and
 21316              needs to be recomputed, so don't consider this row as
 21317              unchanged.  This happens when the last line was
 21318              bidi-reordered and was killed immediately before this
 21319              redisplay cycle.  In that case, ROW->end stores the
 21320              buffer position of the first visual-order character of
 21321              the killed text, which is now beyond ZV.  */
 21322           && CHARPOS (row->end.pos) <= ZV)
 21323         row_found = row;
 21324 
 21325       /* Stop if last visible row.  */
 21326       if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
 21327         break;
 21328     }
 21329 
 21330   return row_found;
 21331 }
 21332 
 21333 
 21334 /* Find the first glyph row in the current matrix of W that is not
 21335    affected by changes at the end of current_buffer since the
 21336    time W's current matrix was built.
 21337 
 21338    Return in *DELTA the number of chars by which buffer positions in
 21339    unchanged text at the end of current_buffer must be adjusted.
 21340 
 21341    Return in *DELTA_BYTES the corresponding number of bytes.
 21342 
 21343    Value is null if no such row exists, i.e. all rows are affected by
 21344    changes.  */
 21345 
 21346 static struct glyph_row *
 21347 find_first_unchanged_at_end_row (struct window *w,
 21348                                  ptrdiff_t *delta, ptrdiff_t *delta_bytes)
 21349 {
 21350   struct glyph_row *row;
 21351   struct glyph_row *row_found = NULL;
 21352 
 21353   *delta = *delta_bytes = 0;
 21354 
 21355   /* Display must not have been paused, otherwise the current matrix
 21356      is not up to date.  */
 21357   eassert (w->window_end_valid);
 21358 
 21359   /* A value of window_end_pos >= END_UNCHANGED means that the window
 21360      end is in the range of changed text.  If so, there is no
 21361      unchanged row at the end of W's current matrix.  */
 21362   if (w->window_end_pos >= END_UNCHANGED)
 21363     return NULL;
 21364 
 21365   /* Set row to the last row in W's current matrix displaying text.  */
 21366   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21367 
 21368   /* If matrix is entirely empty, no unchanged row exists.  */
 21369   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21370     {
 21371       /* The value of row is the last glyph row in the matrix having a
 21372          meaningful buffer position in it.  The end position of row
 21373          corresponds to window_end_pos.  This allows us to translate
 21374          buffer positions in the current matrix to current buffer
 21375          positions for characters not in changed text.  */
 21376       ptrdiff_t Z_old =
 21377         MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21378       ptrdiff_t Z_BYTE_old =
 21379         MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21380       ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
 21381       struct glyph_row *first_text_row
 21382         = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21383 
 21384       *delta = Z - Z_old;
 21385       *delta_bytes = Z_BYTE - Z_BYTE_old;
 21386 
 21387       /* Set last_unchanged_pos to the buffer position of the last
 21388          character in the buffer that has not been changed.  Z is the
 21389          index + 1 of the last character in current_buffer, i.e. by
 21390          subtracting END_UNCHANGED we get the index of the last
 21391          unchanged character, and we have to add BEG to get its buffer
 21392          position.  */
 21393       last_unchanged_pos = Z - END_UNCHANGED + BEG;
 21394       last_unchanged_pos_old = last_unchanged_pos - *delta;
 21395 
 21396       /* Search backward from ROW for a row displaying a line that
 21397          starts at a minimum position >= last_unchanged_pos_old.  */
 21398       for (; row > first_text_row; --row)
 21399         {
 21400           /* This used to abort, but it can happen.
 21401              It is ok to just stop the search instead here.  KFS.  */
 21402           if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21403             break;
 21404 
 21405           if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
 21406             row_found = row;
 21407         }
 21408     }
 21409 
 21410   eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
 21411 
 21412   return row_found;
 21413 }
 21414 
 21415 
 21416 /* Make sure that glyph rows in the current matrix of window W
 21417    reference the same glyph memory as corresponding rows in the
 21418    frame's frame matrix.  This function is called after scrolling W's
 21419    current matrix on a terminal frame in try_window_id and
 21420    try_window_reusing_current_matrix.  */
 21421 
 21422 static void
 21423 sync_frame_with_window_matrix_rows (struct window *w)
 21424 {
 21425   struct frame *f = XFRAME (w->frame);
 21426   struct glyph_row *window_row, *window_row_end, *frame_row;
 21427 
 21428   /* Preconditions: W must be a leaf window and full-width.  Its frame
 21429      must have a frame matrix.  */
 21430   eassert (BUFFERP (w->contents));
 21431   eassert (WINDOW_FULL_WIDTH_P (w));
 21432   eassert (!FRAME_WINDOW_P (f));
 21433 
 21434   /* If W is a full-width window, glyph pointers in W's current matrix
 21435      have, by definition, to be the same as glyph pointers in the
 21436      corresponding frame matrix.  Note that frame matrices have no
 21437      marginal areas (see build_frame_matrix).  */
 21438   window_row = w->current_matrix->rows;
 21439   window_row_end = window_row + w->current_matrix->nrows;
 21440   frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
 21441   while (window_row < window_row_end)
 21442     {
 21443       struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
 21444       struct glyph *end = window_row->glyphs[LAST_AREA];
 21445 
 21446       frame_row->glyphs[LEFT_MARGIN_AREA] = start;
 21447       frame_row->glyphs[TEXT_AREA] = start;
 21448       frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
 21449       frame_row->glyphs[LAST_AREA] = end;
 21450 
 21451       /* Disable frame rows whose corresponding window rows have
 21452          been disabled in try_window_id.  */
 21453       if (!window_row->enabled_p)
 21454         frame_row->enabled_p = false;
 21455 
 21456       ++window_row, ++frame_row;
 21457     }
 21458 }
 21459 
 21460 
 21461 /* Find the glyph row in window W containing CHARPOS.  Consider all
 21462    rows between START and END (not inclusive).  END null means search
 21463    all rows to the end of the display area of W.  Value is the row
 21464    containing CHARPOS or null.  */
 21465 
 21466 struct glyph_row *
 21467 row_containing_pos (struct window *w, ptrdiff_t charpos,
 21468                     struct glyph_row *start, struct glyph_row *end, int dy)
 21469 {
 21470   struct glyph_row *row = start;
 21471   struct glyph_row *best_row = NULL;
 21472   ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
 21473   int last_y;
 21474 
 21475   /* If we happen to start on a header-line or a tab-line, skip that.  */
 21476   if (row->tab_line_p)
 21477     ++row;
 21478   if (row->mode_line_p)
 21479     ++row;
 21480 
 21481   if ((end && row >= end) || !row->enabled_p)
 21482     return NULL;
 21483 
 21484   last_y = window_text_bottom_y (w) - dy;
 21485 
 21486   while (true)
 21487     {
 21488       /* Give up if we have gone too far.  */
 21489       if ((end && row >= end) || !row->enabled_p)
 21490         return NULL;
 21491       /* This formerly returned if they were equal.
 21492          I think that both quantities are of a "last plus one" type;
 21493          if so, when they are equal, the row is within the screen. -- rms.  */
 21494       if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
 21495         return NULL;
 21496 
 21497       /* If it is in this row, return this row.  */
 21498       if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
 21499              || (MATRIX_ROW_END_CHARPOS (row) == charpos
 21500                  /* The end position of a row equals the start
 21501                     position of the next row.  If CHARPOS is there, we
 21502                     would rather consider it displayed in the next
 21503                     line, except when this line ends in ZV.  */
 21504                  && !row_for_charpos_p (row, charpos)))
 21505           && charpos >= MATRIX_ROW_START_CHARPOS (row))
 21506         {
 21507           struct glyph *g;
 21508 
 21509           if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21510               || (!best_row && !row->continued_p))
 21511             return row;
 21512           /* In bidi-reordered rows, there could be several rows whose
 21513              edges surround CHARPOS, all of these rows belonging to
 21514              the same continued line.  We need to find the row which
 21515              fits CHARPOS the best.  */
 21516           for (g = row->glyphs[TEXT_AREA];
 21517                g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21518                g++)
 21519             {
 21520               if (!STRINGP (g->object))
 21521                 {
 21522                   if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
 21523                     {
 21524                       mindif = eabs (g->charpos - charpos);
 21525                       best_row = row;
 21526                       /* Exact match always wins.  */
 21527                       if (mindif == 0)
 21528                         return best_row;
 21529                     }
 21530                 }
 21531             }
 21532         }
 21533       else if (best_row && !row->continued_p)
 21534         return best_row;
 21535       ++row;
 21536     }
 21537 }
 21538 
 21539 
 21540 /* Try to redisplay window W by reusing its existing display.  W's
 21541    current matrix must be up to date when this function is called,
 21542    i.e., window_end_valid must be true.
 21543 
 21544    Value is
 21545 
 21546    >= 1 if successful, i.e. display has been updated
 21547          specifically:
 21548          1 means the changes were in front of a newline that precedes
 21549            the window start, and the whole current matrix was reused
 21550          2 means the changes were after the last position displayed
 21551            in the window, and the whole current matrix was reused
 21552          3 means portions of the current matrix were reused, while
 21553            some of the screen lines were redrawn
 21554    -1   if redisplay with same window start is known not to succeed
 21555    0    if otherwise unsuccessful
 21556 
 21557    The following steps are performed:
 21558 
 21559    1. Find the last row in the current matrix of W that is not
 21560    affected by changes at the start of current_buffer.  If no such row
 21561    is found, give up.
 21562 
 21563    2. Find the first row in W's current matrix that is not affected by
 21564    changes at the end of current_buffer.  Maybe there is no such row.
 21565 
 21566    3. Display lines beginning with the row + 1 found in step 1 to the
 21567    row found in step 2 or, if step 2 didn't find a row, to the end of
 21568    the window.
 21569 
 21570    4. If cursor is not known to appear on the window, give up.
 21571 
 21572    5. If display stopped at the row found in step 2, scroll the
 21573    display and current matrix as needed.
 21574 
 21575    6. Maybe display some lines at the end of W, if we must.  This can
 21576    happen under various circumstances, like a partially visible line
 21577    becoming fully visible, or because newly displayed lines are displayed
 21578    in smaller font sizes.
 21579 
 21580    7. Update W's window end information.  */
 21581 
 21582 static int
 21583 try_window_id (struct window *w)
 21584 {
 21585   struct frame *f = XFRAME (w->frame);
 21586   struct glyph_matrix *current_matrix = w->current_matrix;
 21587   struct glyph_matrix *desired_matrix = w->desired_matrix;
 21588   struct glyph_row *last_unchanged_at_beg_row;
 21589   struct glyph_row *first_unchanged_at_end_row;
 21590   struct glyph_row *row;
 21591   struct glyph_row *bottom_row;
 21592   int bottom_vpos;
 21593   struct it it;
 21594   ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
 21595   int dvpos, dy;
 21596   struct text_pos start_pos;
 21597   struct run run;
 21598   int first_unchanged_at_end_vpos = 0;
 21599   struct glyph_row *last_text_row, *last_text_row_at_end;
 21600   struct text_pos start;
 21601   ptrdiff_t first_changed_charpos, last_changed_charpos;
 21602 
 21603 #ifdef GLYPH_DEBUG
 21604   if (inhibit_try_window_id)
 21605     return 0;
 21606 #endif
 21607 
 21608   /* This is handy for debugging.  */
 21609 #if false
 21610 #define GIVE_UP(X)                                              \
 21611   do {                                                          \
 21612     redisplay_trace ("try_window_id give up %d\n", X);          \
 21613     return 0;                                                   \
 21614   } while (false)
 21615 #else
 21616 #define GIVE_UP(X) return 0
 21617 #endif
 21618 
 21619   SET_TEXT_POS_FROM_MARKER (start, w->start);
 21620 
 21621   /* Don't use this for mini-windows because these can show
 21622      messages and mini-buffers, and we don't handle that here.  */
 21623   if (MINI_WINDOW_P (w))
 21624     GIVE_UP (1);
 21625 
 21626   /* This flag is used to prevent redisplay optimizations.  */
 21627   if (windows_or_buffers_changed || f->cursor_type_changed)
 21628     GIVE_UP (2);
 21629 
 21630   /* This function's optimizations cannot be used if overlays have
 21631      changed in the buffer displayed by the window, so give up if they
 21632      have.  */
 21633   if (w->last_overlay_modified != OVERLAY_MODIFF)
 21634     GIVE_UP (200);
 21635 
 21636   /* Verify that narrowing has not changed.
 21637      Also verify that we were not told to prevent redisplay optimizations.
 21638      It would be nice to further
 21639      reduce the number of cases where this prevents try_window_id.  */
 21640   if (current_buffer->clip_changed
 21641       || current_buffer->prevent_redisplay_optimizations_p)
 21642     GIVE_UP (3);
 21643 
 21644   /* Window must either use window-based redisplay or be full width.  */
 21645   if (!FRAME_WINDOW_P (f)
 21646       && (!FRAME_LINE_INS_DEL_OK (f)
 21647           || !WINDOW_FULL_WIDTH_P (w)))
 21648     GIVE_UP (4);
 21649 
 21650   /* Give up if point is known NOT to appear in W.  */
 21651   if (PT < CHARPOS (start))
 21652     GIVE_UP (5);
 21653 
 21654   /* Another way to prevent redisplay optimizations.  */
 21655   if (w->last_modified == 0)
 21656     GIVE_UP (6);
 21657 
 21658   /* Verify that window is not hscrolled.  */
 21659   if (w->hscroll != 0)
 21660     GIVE_UP (7);
 21661 
 21662   /* Verify that display wasn't paused.  */
 21663   if (!w->window_end_valid)
 21664     GIVE_UP (8);
 21665 
 21666   /* Likewise if highlighting trailing whitespace.  */
 21667   if (!NILP (Vshow_trailing_whitespace))
 21668     GIVE_UP (11);
 21669 
 21670   /* Can't use this if overlay arrow position and/or string have
 21671      changed.  */
 21672   if (overlay_arrows_changed_p (false))
 21673     GIVE_UP (12);
 21674 
 21675   /* When word-wrap is on, adding a space to the first word of a
 21676      wrapped line can change the wrap position, altering the line
 21677      above it.  It might be worthwhile to handle this more
 21678      intelligently, but for now just redisplay from scratch.  */
 21679   if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
 21680     GIVE_UP (21);
 21681 
 21682   /* Under bidi reordering, adding or deleting a character in the
 21683      beginning of a paragraph, before the first strong directional
 21684      character, can change the base direction of the paragraph (unless
 21685      the buffer specifies a fixed paragraph direction), which will
 21686      require redisplaying the whole paragraph.  It might be worthwhile
 21687      to find the paragraph limits and widen the range of redisplayed
 21688      lines to that, but for now just give up this optimization and
 21689      redisplay from scratch.  */
 21690   if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21691       && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 21692     GIVE_UP (22);
 21693 
 21694   /* Give up if the buffer has line-spacing set, as Lisp-level changes
 21695      to that variable require thorough redisplay.  */
 21696   if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
 21697     GIVE_UP (23);
 21698 
 21699   /* Give up if display-line-numbers is in relative mode, or when the
 21700      current line's number needs to be displayed in a distinct face.  */
 21701   if (EQ (Vdisplay_line_numbers, Qrelative)
 21702       || EQ (Vdisplay_line_numbers, Qvisual)
 21703       || (!NILP (Vdisplay_line_numbers)
 21704           && NILP (Finternal_lisp_face_equal_p (Qline_number,
 21705                                                 Qline_number_current_line,
 21706                                                 w->frame))))
 21707     GIVE_UP (24);
 21708 
 21709   /* composition-break-at-point is incompatible with the optimizations
 21710      in this function, because we need to recompose characters when
 21711      point moves off their positions.  */
 21712   if (composition_break_at_point)
 21713     GIVE_UP (27);
 21714 
 21715   /* Make sure beg_unchanged and end_unchanged are up to date.  Do it
 21716      only if buffer has really changed.  The reason is that the gap is
 21717      initially at Z for freshly visited files.  The code below would
 21718      set end_unchanged to 0 in that case.  */
 21719   if (MODIFF > SAVE_MODIFF
 21720       /* This seems to happen sometimes after saving a buffer.  */
 21721       || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
 21722     {
 21723       if (GPT - BEG < BEG_UNCHANGED)
 21724         BEG_UNCHANGED = GPT - BEG;
 21725       if (Z - GPT < END_UNCHANGED)
 21726         END_UNCHANGED = Z - GPT;
 21727     }
 21728 
 21729   /* The position of the first and last character that has been changed.  */
 21730   first_changed_charpos = BEG + BEG_UNCHANGED;
 21731   last_changed_charpos  = Z - END_UNCHANGED;
 21732 
 21733   /* If window starts after a line end, and the last change is in
 21734      front of that newline, then changes don't affect the display.
 21735      This case happens with stealth-fontification.  Note that although
 21736      the display is unchanged, glyph positions in the matrix have to
 21737      be adjusted, of course.  */
 21738   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21739   if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21740       && ((last_changed_charpos < CHARPOS (start)
 21741            && CHARPOS (start) == BEGV)
 21742           || (last_changed_charpos < CHARPOS (start) - 1
 21743               && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
 21744     {
 21745       ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
 21746       struct glyph_row *r0;
 21747 
 21748       /* Compute how many chars/bytes have been added to or removed
 21749          from the buffer.  */
 21750       Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21751       Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21752       Z_delta = Z - Z_old;
 21753       Z_delta_bytes = Z_BYTE - Z_BYTE_old;
 21754 
 21755       /* Give up if PT is not in the window.  Note that it already has
 21756          been checked at the start of try_window_id that PT is not in
 21757          front of the window start.  */
 21758       if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
 21759         GIVE_UP (13);
 21760 
 21761       /* If window start is unchanged, we can reuse the whole matrix
 21762          as is, after adjusting glyph positions.  No need to compute
 21763          the window end again, since its offset from Z hasn't changed.  */
 21764       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21765       if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
 21766           && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
 21767           /* PT must not be in a partially visible line.  */
 21768           && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
 21769                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 21770         {
 21771           /* Adjust positions in the glyph matrix.  */
 21772           if (Z_delta || Z_delta_bytes)
 21773             {
 21774               struct glyph_row *r1
 21775                 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 21776               increment_matrix_positions (w->current_matrix,
 21777                                           MATRIX_ROW_VPOS (r0, current_matrix),
 21778                                           MATRIX_ROW_VPOS (r1, current_matrix),
 21779                                           Z_delta, Z_delta_bytes);
 21780             }
 21781 
 21782           /* Set the cursor.  */
 21783           row = row_containing_pos (w, PT, r0, NULL, 0);
 21784           if (row)
 21785             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 21786           return 1;
 21787         }
 21788     }
 21789 
 21790   /* Handle the case that changes are all below what is displayed in
 21791      the window, and that PT is in the window.  This shortcut cannot
 21792      be taken if ZV is visible in the window, and text has been added
 21793      there that is visible in the window.  */
 21794   if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
 21795       /* ZV is not visible in the window, or there are no
 21796          changes at ZV, actually.  */
 21797       && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
 21798           || first_changed_charpos == last_changed_charpos))
 21799     {
 21800       struct glyph_row *r0;
 21801 
 21802       /* Give up if PT is not in the window.  Note that it already has
 21803          been checked at the start of try_window_id that PT is not in
 21804          front of the window start.  */
 21805       if (PT >= MATRIX_ROW_END_CHARPOS (row))
 21806         GIVE_UP (14);
 21807 
 21808       /* If window start is unchanged, we can reuse the whole matrix
 21809          as is, without changing glyph positions since no text has
 21810          been added/removed in front of the window end.  */
 21811       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21812       if (TEXT_POS_EQUAL_P (start, r0->minpos)
 21813           /* PT must not be in a partially visible line.  */
 21814           && !(PT >= MATRIX_ROW_START_CHARPOS (row)
 21815                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 21816         {
 21817           /* We have to compute the window end anew since text
 21818              could have been added/removed after it.  */
 21819           w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 21820           w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 21821 
 21822           /* Set the cursor.  */
 21823           row = row_containing_pos (w, PT, r0, NULL, 0);
 21824           if (row)
 21825             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 21826           return 2;
 21827         }
 21828     }
 21829 
 21830   /* Give up if window start is in the changed area.
 21831 
 21832      The condition used to read
 21833 
 21834      (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
 21835 
 21836      but why that was tested escapes me at the moment.  */
 21837   if (CHARPOS (start) >= first_changed_charpos
 21838       && CHARPOS (start) <= last_changed_charpos)
 21839     GIVE_UP (15);
 21840 
 21841   /* Check that window start agrees with the start of the first glyph
 21842      row in its current matrix.  Check this after we know the window
 21843      start is not in changed text, otherwise positions would not be
 21844      comparable.  */
 21845   row = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21846   if (!TEXT_POS_EQUAL_P (start, row->minpos))
 21847     GIVE_UP (16);
 21848 
 21849   /* Give up if the window ends in strings.  Overlay strings
 21850      at the end are difficult to handle, so don't try.  */
 21851   row = MATRIX_ROW (current_matrix, w->window_end_vpos);
 21852   if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
 21853     GIVE_UP (20);
 21854 
 21855   /* Can't let scroll_run_hook below run on w32 GUI frames when
 21856      position of point is indicated by the system caret, because
 21857      scrolling the display will then "copy" the pixels used by the
 21858      caret.  */
 21859 #ifdef HAVE_NTGUI
 21860   if (FRAME_W32_P (f) && w32_use_visible_system_caret)
 21861     GIVE_UP (25);
 21862 #endif
 21863 
 21864   /* Compute the position at which we have to start displaying new
 21865      lines.  Some of the lines at the top of the window might be
 21866      reusable because they are not displaying changed text.  Find the
 21867      last row in W's current matrix not affected by changes at the
 21868      start of current_buffer.  Value is null if changes start in the
 21869      first line of window.  */
 21870   last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
 21871   if (last_unchanged_at_beg_row)
 21872     {
 21873       /* Avoid starting to display in the middle of a character, a TAB
 21874          for instance.  This is easier than to set up the iterator
 21875          exactly, and it's not a frequent case, so the additional
 21876          effort wouldn't really pay off.  */
 21877       while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
 21878               || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
 21879              && last_unchanged_at_beg_row > w->current_matrix->rows)
 21880         --last_unchanged_at_beg_row;
 21881 
 21882       if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
 21883         GIVE_UP (17);
 21884 
 21885       if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
 21886         GIVE_UP (18);
 21887       /* Give up if the row starts with a display property that draws
 21888          on the fringes, since that could prevent correct display of
 21889          line-prefix and wrap-prefix.  */
 21890       if (it.sp > 1
 21891           && it.method == GET_FROM_IMAGE && it.image_id == -1)
 21892         GIVE_UP (26);
 21893       start_pos = it.current.pos;
 21894 
 21895       /* Start displaying new lines in the desired matrix at the same
 21896          vpos we would use in the current matrix, i.e. below
 21897          last_unchanged_at_beg_row.  */
 21898       it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
 21899                                      current_matrix);
 21900       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 21901       it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
 21902 
 21903       eassert (it.hpos == 0 && it.current_x == 0);
 21904     }
 21905   else
 21906     {
 21907       /* There are no reusable lines at the start of the window.
 21908          Start displaying in the first text line.  */
 21909       start_display (&it, w, start);
 21910       it.vpos = it.first_vpos;
 21911       start_pos = it.current.pos;
 21912     }
 21913 
 21914   /* Find the first row that is not affected by changes at the end of
 21915      the buffer.  Value will be null if there is no unchanged row, in
 21916      which case we must redisplay to the end of the window.  delta
 21917      will be set to the value by which buffer positions beginning with
 21918      first_unchanged_at_end_row have to be adjusted due to text
 21919      changes.  */
 21920   first_unchanged_at_end_row
 21921     = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
 21922   IF_DEBUG (debug_delta = delta);
 21923   IF_DEBUG (debug_delta_bytes = delta_bytes);
 21924 
 21925   /* Set stop_pos to the buffer position up to which we will have to
 21926      display new lines.  If first_unchanged_at_end_row != NULL, this
 21927      is the buffer position of the start of the line displayed in that
 21928      row.  For first_unchanged_at_end_row == NULL, use 0 to indicate
 21929      that we don't stop at a buffer position.  */
 21930   stop_pos = 0;
 21931   if (first_unchanged_at_end_row)
 21932     {
 21933       eassert (last_unchanged_at_beg_row == NULL
 21934                || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
 21935 
 21936       /* If this is a continuation line, move forward to the next one
 21937          that isn't.  Changes in lines above affect this line.
 21938          Caution: this may move first_unchanged_at_end_row to a row
 21939          not displaying text.  */
 21940       while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
 21941              && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 21942              && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 21943                  < it.last_visible_y))
 21944         ++first_unchanged_at_end_row;
 21945 
 21946       if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 21947           || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 21948               >= it.last_visible_y))
 21949         first_unchanged_at_end_row = NULL;
 21950       else
 21951         {
 21952           stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
 21953                       + delta);
 21954           first_unchanged_at_end_vpos
 21955             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
 21956           eassert (stop_pos >= Z - END_UNCHANGED);
 21957         }
 21958     }
 21959   else if (last_unchanged_at_beg_row == NULL)
 21960     GIVE_UP (19);
 21961 
 21962 
 21963 #ifdef GLYPH_DEBUG
 21964 
 21965   /* Either there is no unchanged row at the end, or the one we have
 21966      now displays text.  This is a necessary condition for the window
 21967      end pos calculation at the end of this function.  */
 21968   eassert (first_unchanged_at_end_row == NULL
 21969            || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 21970 
 21971   debug_last_unchanged_at_beg_vpos
 21972     = (last_unchanged_at_beg_row
 21973        ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
 21974        : -1);
 21975   debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
 21976 
 21977 #endif /* GLYPH_DEBUG */
 21978 
 21979 
 21980   /* Display new lines.  Set last_text_row to the last new line
 21981      displayed which has text on it, i.e. might end up as being the
 21982      line where the window_end_vpos is.  */
 21983   w->cursor.vpos = -1;
 21984   last_text_row = NULL;
 21985   overlay_arrow_seen = false;
 21986   if (it.current_y < it.last_visible_y
 21987       && !f->fonts_changed
 21988       && (first_unchanged_at_end_row == NULL
 21989           || IT_CHARPOS (it) < stop_pos))
 21990     it.glyph_row->reversed_p = false;
 21991   while (it.current_y < it.last_visible_y
 21992          && !f->fonts_changed
 21993          && (first_unchanged_at_end_row == NULL
 21994              || IT_CHARPOS (it) < stop_pos))
 21995     {
 21996       if (display_line (&it, -1))
 21997         last_text_row = it.glyph_row - 1;
 21998     }
 21999 
 22000   if (f->fonts_changed)
 22001     return -1;
 22002 
 22003   /* The redisplay iterations in display_line above could have
 22004      triggered font-lock, which could have done something that
 22005      invalidates IT->w window's end-point information, on which we
 22006      rely below.  E.g., one package, which will remain unnamed, used
 22007      to install a font-lock-fontify-region-function that called
 22008      bury-buffer, whose side effect is to switch the buffer displayed
 22009      by IT->w, and that predictably resets IT->w's window_end_valid
 22010      flag, which we already tested at the entry to this function.
 22011      Amply punish such packages/modes by giving up on this
 22012      optimization in those cases.  */
 22013   if (!w->window_end_valid)
 22014     {
 22015       clear_glyph_matrix (w->desired_matrix);
 22016       return -1;
 22017     }
 22018 
 22019   /* Compute differences in buffer positions, y-positions etc.  for
 22020      lines reused at the bottom of the window.  Compute what we can
 22021      scroll.  */
 22022   if (first_unchanged_at_end_row
 22023       /* No lines reused because we displayed everything up to the
 22024          bottom of the window.  */
 22025       && it.current_y < it.last_visible_y)
 22026     {
 22027       dvpos = (it.vpos
 22028                - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
 22029                                   current_matrix));
 22030       dy = it.current_y - first_unchanged_at_end_row->y;
 22031       run.current_y = first_unchanged_at_end_row->y;
 22032       run.desired_y = run.current_y + dy;
 22033       run.height = it.last_visible_y - max (run.current_y, run.desired_y);
 22034     }
 22035   else
 22036     {
 22037       delta = delta_bytes = dvpos = dy
 22038         = run.current_y = run.desired_y = run.height = 0;
 22039       first_unchanged_at_end_row = NULL;
 22040     }
 22041   IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
 22042 
 22043 
 22044   /* Find the cursor if not already found.  We have to decide whether
 22045      PT will appear on this window (it sometimes doesn't, but this is
 22046      not a very frequent case.)  This decision has to be made before
 22047      the current matrix is altered.  A value of cursor.vpos < 0 means
 22048      that PT is either in one of the lines beginning at
 22049      first_unchanged_at_end_row or below the window.  Don't care for
 22050      lines that might be displayed later at the window end; as
 22051      mentioned, this is not a frequent case.  */
 22052   if (w->cursor.vpos < 0)
 22053     {
 22054       /* Cursor in unchanged rows at the top?  */
 22055       if (PT < CHARPOS (start_pos)
 22056           && last_unchanged_at_beg_row)
 22057         {
 22058           row = row_containing_pos (w, PT,
 22059                                     MATRIX_FIRST_TEXT_ROW (w->current_matrix),
 22060                                     last_unchanged_at_beg_row + 1, 0);
 22061           if (row)
 22062             set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 22063         }
 22064 
 22065       /* Start from first_unchanged_at_end_row looking for PT.  */
 22066       else if (first_unchanged_at_end_row)
 22067         {
 22068           row = row_containing_pos (w, PT - delta,
 22069                                     first_unchanged_at_end_row, NULL, 0);
 22070           if (row)
 22071             set_cursor_from_row (w, row, w->current_matrix, delta,
 22072                                  delta_bytes, dy, dvpos);
 22073         }
 22074 
 22075       /* Give up if cursor was not found.  */
 22076       if (w->cursor.vpos < 0)
 22077         {
 22078           clear_glyph_matrix (w->desired_matrix);
 22079           return -1;
 22080         }
 22081     }
 22082 
 22083   /* Don't let the cursor end in the scroll margins.  */
 22084   {
 22085     int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 22086     int bot_scroll_margin = top_scroll_margin;
 22087     int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
 22088 
 22089     if (window_wants_header_line (w))
 22090       top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 22091     if (window_wants_tab_line (w))
 22092       top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 22093 
 22094     if ((w->cursor.y < top_scroll_margin
 22095          && CHARPOS (start) > BEGV)
 22096         /* Old redisplay didn't take scroll margin into account at the bottom,
 22097            but then global-hl-line-mode doesn't scroll.  KFS 2004-06-14 */
 22098         || (w->cursor.y
 22099             + (cursor_row_fully_visible_p (w, false, true, true)
 22100                ? 1
 22101                : cursor_height + bot_scroll_margin)) > it.last_visible_y)
 22102       {
 22103         w->cursor.vpos = -1;
 22104         clear_glyph_matrix (w->desired_matrix);
 22105         return -1;
 22106       }
 22107   }
 22108 
 22109   /* Scroll the display.  Do it before changing the current matrix so
 22110      that xterm.c doesn't get confused about where the cursor glyph is
 22111      found.  */
 22112   if (dy && run.height)
 22113     {
 22114       update_begin (f);
 22115 
 22116       if (FRAME_WINDOW_P (f))
 22117         {
 22118 #ifdef HAVE_WINDOW_SYSTEM
 22119           gui_update_window_begin (w);
 22120           FRAME_RIF (f)->clear_window_mouse_face (w);
 22121           FRAME_RIF (f)->scroll_run_hook (w, &run);
 22122           gui_update_window_end (w, false, false);
 22123 #endif
 22124         }
 22125       else
 22126         {
 22127           /* Terminal frame.  In this case, dvpos gives the number of
 22128              lines to scroll by; dvpos < 0 means scroll up.  */
 22129           int from_vpos
 22130             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
 22131           int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
 22132           int end = (WINDOW_TOP_EDGE_LINE (w)
 22133                      + window_wants_tab_line (w)
 22134                      + window_wants_header_line (w)
 22135                      + window_internal_height (w));
 22136 
 22137           gui_clear_window_mouse_face (w);
 22138 
 22139           /* Perform the operation on the screen.  */
 22140           if (dvpos > 0)
 22141             {
 22142               /* Scroll last_unchanged_at_beg_row to the end of the
 22143                  window down dvpos lines.  */
 22144               set_terminal_window (f, end);
 22145 
 22146               /* On dumb terminals delete dvpos lines at the end
 22147                  before inserting dvpos empty lines.  */
 22148               if (!FRAME_SCROLL_REGION_OK (f))
 22149                 ins_del_lines (f, end - dvpos, -dvpos);
 22150 
 22151               /* Insert dvpos empty lines in front of
 22152                  last_unchanged_at_beg_row.  */
 22153               ins_del_lines (f, from, dvpos);
 22154             }
 22155           else if (dvpos < 0)
 22156             {
 22157               /* Scroll up last_unchanged_at_beg_vpos to the end of
 22158                  the window to last_unchanged_at_beg_vpos - |dvpos|.  */
 22159               set_terminal_window (f, end);
 22160 
 22161               /* Delete dvpos lines in front of
 22162                  last_unchanged_at_beg_vpos.  ins_del_lines will set
 22163                  the cursor to the given vpos and emit |dvpos| delete
 22164                  line sequences.  */
 22165               ins_del_lines (f, from + dvpos, dvpos);
 22166 
 22167               /* On a dumb terminal insert dvpos empty lines at the
 22168                  end.  */
 22169               if (!FRAME_SCROLL_REGION_OK (f))
 22170                 ins_del_lines (f, end + dvpos, -dvpos);
 22171             }
 22172 
 22173           set_terminal_window (f, 0);
 22174         }
 22175 
 22176       update_end (f);
 22177     }
 22178 
 22179   /* Shift reused rows of the current matrix to the right position.
 22180      BOTTOM_ROW is the last + 1 row in the current matrix reserved for
 22181      text.  */
 22182   bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22183   bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
 22184   if (dvpos < 0)
 22185     {
 22186       rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
 22187                      bottom_vpos, dvpos);
 22188       clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
 22189                                bottom_vpos);
 22190     }
 22191   else if (dvpos > 0)
 22192     {
 22193       rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
 22194                      bottom_vpos, dvpos);
 22195       clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
 22196                                first_unchanged_at_end_vpos + dvpos);
 22197     }
 22198 
 22199   /* For frame-based redisplay, make sure that current frame and window
 22200      matrix are in sync with respect to glyph memory.  */
 22201   if (!FRAME_WINDOW_P (f))
 22202     sync_frame_with_window_matrix_rows (w);
 22203 
 22204   /* Adjust buffer positions in reused rows.  */
 22205   if (delta || delta_bytes)
 22206     increment_matrix_positions (current_matrix,
 22207                                 first_unchanged_at_end_vpos + dvpos,
 22208                                 bottom_vpos, delta, delta_bytes);
 22209 
 22210   /* Adjust Y positions.  */
 22211   if (dy)
 22212     shift_glyph_matrix (w, current_matrix,
 22213                         first_unchanged_at_end_vpos + dvpos,
 22214                         bottom_vpos, dy);
 22215 
 22216   if (first_unchanged_at_end_row)
 22217     {
 22218       first_unchanged_at_end_row += dvpos;
 22219       if (first_unchanged_at_end_row->y >= it.last_visible_y
 22220           || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
 22221         first_unchanged_at_end_row = NULL;
 22222     }
 22223 
 22224   /* If scrolling up, there may be some lines to display at the end of
 22225      the window.  */
 22226   last_text_row_at_end = NULL;
 22227   if (dy < 0)
 22228     {
 22229       /* Scrolling up can leave for example a partially visible line
 22230          at the end of the window to be redisplayed.  */
 22231       /* Set last_row to the glyph row in the current matrix where the
 22232          window end line is found.  It has been moved up or down in
 22233          the matrix by dvpos.  */
 22234       int last_vpos = w->window_end_vpos + dvpos;
 22235       struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
 22236 
 22237       /* If last_row is the window end line, it should display text.  */
 22238       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
 22239 
 22240       /* If window end line was partially visible before, begin
 22241          displaying at that line.  Otherwise begin displaying with the
 22242          line following it.  */
 22243       if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
 22244         {
 22245           init_to_row_start (&it, w, last_row);
 22246           it.vpos = last_vpos;
 22247           it.current_y = last_row->y;
 22248         }
 22249       else
 22250         {
 22251           init_to_row_end (&it, w, last_row);
 22252           it.vpos = 1 + last_vpos;
 22253           it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
 22254           ++last_row;
 22255         }
 22256 
 22257       /* We may start in a continuation line.  If so, we have to
 22258          get the right continuation_lines_width and current_x.  */
 22259       it.continuation_lines_width = last_row->continuation_lines_width;
 22260       it.hpos = it.current_x = 0;
 22261 
 22262       /* Display the rest of the lines at the window end.  */
 22263       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22264       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 22265         {
 22266           /* Is it always sure that the display agrees with lines in
 22267              the current matrix?  I don't think so, so we mark rows
 22268              displayed invalid in the current matrix by setting their
 22269              enabled_p flag to false.  */
 22270           SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
 22271           if (display_line (&it, w->cursor.vpos))
 22272             last_text_row_at_end = it.glyph_row - 1;
 22273         }
 22274     }
 22275 
 22276   /* Update window_end_pos and window_end_vpos.  */
 22277   if (first_unchanged_at_end_row && !last_text_row_at_end)
 22278     {
 22279       /* Window end line if one of the preserved rows from the current
 22280          matrix.  Set row to the last row displaying text in current
 22281          matrix starting at first_unchanged_at_end_row, after
 22282          scrolling.  */
 22283       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22284       row = find_last_row_displaying_text (w->current_matrix, &it,
 22285                                            first_unchanged_at_end_row);
 22286       eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
 22287       adjust_window_ends (w, row, true);
 22288       eassert (w->window_end_bytepos >= 0);
 22289       IF_DEBUG (debug_method_add (w, "A"));
 22290     }
 22291   else if (last_text_row_at_end)
 22292     {
 22293       adjust_window_ends (w, last_text_row_at_end, false);
 22294       eassert (w->window_end_bytepos >= 0);
 22295       IF_DEBUG (debug_method_add (w, "B"));
 22296     }
 22297   else if (last_text_row)
 22298     {
 22299       /* We have displayed either to the end of the window or at the
 22300          end of the window, i.e. the last row with text is to be found
 22301          in the desired matrix.  */
 22302       adjust_window_ends (w, last_text_row, false);
 22303       eassert (w->window_end_bytepos >= 0);
 22304     }
 22305   else if (first_unchanged_at_end_row == NULL
 22306            && last_text_row == NULL
 22307            && last_text_row_at_end == NULL)
 22308     {
 22309       /* Displayed to end of window, but no line containing text was
 22310          displayed.  Lines were deleted at the end of the window.  */
 22311       int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
 22312       int vpos = w->window_end_vpos;
 22313       struct glyph_row *current_row = current_matrix->rows + vpos;
 22314       struct glyph_row *desired_row = desired_matrix->rows + vpos;
 22315 
 22316       for (row = NULL; !row; --vpos, --current_row, --desired_row)
 22317         {
 22318           eassert (first_vpos <= vpos);
 22319           if (desired_row->enabled_p)
 22320             {
 22321               if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
 22322                 row = desired_row;
 22323             }
 22324           else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
 22325             row  = current_row;
 22326         }
 22327 
 22328       w->window_end_vpos = vpos + 1;
 22329       w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22330       w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22331       eassert (w->window_end_bytepos >= 0);
 22332       IF_DEBUG (debug_method_add (w, "C"));
 22333     }
 22334   else
 22335     emacs_abort ();
 22336 
 22337   IF_DEBUG ((debug_end_pos = w->window_end_pos,
 22338              debug_end_vpos = w->window_end_vpos));
 22339 
 22340   /* Record that display has not been completed.  */
 22341   w->window_end_valid = false;
 22342   w->desired_matrix->no_scrolling_p = true;
 22343   return 3;
 22344 
 22345 #undef GIVE_UP
 22346 }
 22347 
 22348 
 22349 
 22350 /***********************************************************************
 22351                         More debugging support
 22352  ***********************************************************************/
 22353 
 22354 #ifdef GLYPH_DEBUG
 22355 
 22356 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
 22357 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
 22358 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
 22359 
 22360 
 22361 /* Dump the contents of glyph matrix MATRIX on stderr.
 22362 
 22363    GLYPHS 0 means don't show glyph contents.
 22364    GLYPHS 1 means show glyphs in short form
 22365    GLYPHS > 1 means show glyphs in long form.  */
 22366 
 22367 void
 22368 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
 22369 {
 22370   int i;
 22371   for (i = 0; i < matrix->nrows; ++i)
 22372     dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
 22373 }
 22374 
 22375 
 22376 /* Dump contents of glyph GLYPH to stderr.  ROW and AREA are
 22377    the glyph row and area where the glyph comes from.  */
 22378 
 22379 void
 22380 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
 22381 {
 22382   if (glyph->type == CHAR_GLYPH
 22383       || glyph->type == GLYPHLESS_GLYPH)
 22384     {
 22385       fprintf (stderr,
 22386                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22387                glyph - row->glyphs[TEXT_AREA],
 22388                (glyph->type == CHAR_GLYPH
 22389                 ? 'C'
 22390                 : 'G'),
 22391                glyph->charpos,
 22392                (BUFFERP (glyph->object)
 22393                 ? 'B'
 22394                 : (STRINGP (glyph->object)
 22395                    ? 'S'
 22396                    : (NILP (glyph->object)
 22397                       ? '0'
 22398                       : '-'))),
 22399                glyph->pixel_width,
 22400                glyph->u.ch,
 22401                (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
 22402                 ? (int) glyph->u.ch
 22403                 : '.'),
 22404                glyph->face_id,
 22405                glyph->left_box_line_p,
 22406                glyph->right_box_line_p);
 22407     }
 22408   else if (glyph->type == STRETCH_GLYPH)
 22409     {
 22410       fprintf (stderr,
 22411                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22412                glyph - row->glyphs[TEXT_AREA],
 22413                'S',
 22414                glyph->charpos,
 22415                (BUFFERP (glyph->object)
 22416                 ? 'B'
 22417                 : (STRINGP (glyph->object)
 22418                    ? 'S'
 22419                    : (NILP (glyph->object)
 22420                       ? '0'
 22421                       : '-'))),
 22422                glyph->pixel_width,
 22423                0u,
 22424                ' ',
 22425                glyph->face_id,
 22426                glyph->left_box_line_p,
 22427                glyph->right_box_line_p);
 22428     }
 22429   else if (glyph->type == IMAGE_GLYPH)
 22430     {
 22431       fprintf (stderr,
 22432                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22433                glyph - row->glyphs[TEXT_AREA],
 22434                'I',
 22435                glyph->charpos,
 22436                (BUFFERP (glyph->object)
 22437                 ? 'B'
 22438                 : (STRINGP (glyph->object)
 22439                    ? 'S'
 22440                    : (NILP (glyph->object)
 22441                       ? '0'
 22442                       : '-'))),
 22443                glyph->pixel_width,
 22444                (unsigned int) glyph->u.img_id,
 22445                '.',
 22446                glyph->face_id,
 22447                glyph->left_box_line_p,
 22448                glyph->right_box_line_p);
 22449     }
 22450   else if (glyph->type == COMPOSITE_GLYPH)
 22451     {
 22452       fprintf (stderr,
 22453                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x",
 22454                glyph - row->glyphs[TEXT_AREA],
 22455                '+',
 22456                glyph->charpos,
 22457                (BUFFERP (glyph->object)
 22458                 ? 'B'
 22459                 : (STRINGP (glyph->object)
 22460                    ? 'S'
 22461                    : (NILP (glyph->object)
 22462                       ? '0'
 22463                       : '-'))),
 22464                glyph->pixel_width,
 22465                (unsigned int) glyph->u.cmp.id);
 22466       if (glyph->u.cmp.automatic)
 22467         fprintf (stderr,
 22468                  "[%d-%d]",
 22469                  glyph->slice.cmp.from, glyph->slice.cmp.to);
 22470       fprintf (stderr, " . %4d %1.1d%1.1d\n",
 22471                glyph->face_id,
 22472                glyph->left_box_line_p,
 22473                glyph->right_box_line_p);
 22474     }
 22475   else if (glyph->type == XWIDGET_GLYPH)
 22476     {
 22477 #ifndef HAVE_XWIDGETS
 22478       eassume (false);
 22479 #else
 22480       fprintf (stderr,
 22481                "  %5td %4c %6td %c %3d %7p %c %4d %1.1d%1.1d\n",
 22482                glyph - row->glyphs[TEXT_AREA],
 22483                'X',
 22484                glyph->charpos,
 22485                (BUFFERP (glyph->object)
 22486                 ? 'B'
 22487                 : (STRINGP (glyph->object)
 22488                    ? 'S'
 22489                    : '-')),
 22490                glyph->pixel_width,
 22491                glyph->u.xwidget,
 22492                '.',
 22493                glyph->face_id,
 22494                glyph->left_box_line_p,
 22495                glyph->right_box_line_p);
 22496 #endif
 22497     }
 22498 }
 22499 
 22500 
 22501 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
 22502    GLYPHS 0 means don't show glyph contents.
 22503    GLYPHS 1 means show glyphs in short form
 22504    GLYPHS > 1 means show glyphs in long form.  */
 22505 
 22506 void
 22507 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
 22508 {
 22509   if (glyphs != 1)
 22510     {
 22511       fputs (("Row     Start       End Used oE><\\CTZFesm     X    Y    W    H    V    A    P\n"
 22512               "==============================================================================\n"),
 22513              stderr);
 22514       fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
 22515 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d  %4d %4d %4d %4d %4d %4d %4d\n",
 22516                vpos,
 22517                MATRIX_ROW_START_CHARPOS (row),
 22518                MATRIX_ROW_END_CHARPOS (row),
 22519                row->used[TEXT_AREA],
 22520                row->contains_overlapping_glyphs_p,
 22521                row->enabled_p,
 22522                row->truncated_on_left_p,
 22523                row->truncated_on_right_p,
 22524                row->continued_p,
 22525                MATRIX_ROW_CONTINUATION_LINE_P (row),
 22526                MATRIX_ROW_DISPLAYS_TEXT_P (row),
 22527                row->ends_at_zv_p,
 22528                row->fill_line_p,
 22529                row->ends_in_middle_of_char_p,
 22530                row->starts_in_middle_of_char_p,
 22531                row->mouse_face_p,
 22532                row->x,
 22533                row->y,
 22534                row->pixel_width,
 22535                row->height,
 22536                row->visible_height,
 22537                row->ascent,
 22538                row->phys_ascent);
 22539       /* The next 3 lines should align to "Start" in the header.  */
 22540       fprintf (stderr, "    %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
 22541                row->end.overlay_string_index,
 22542                row->continuation_lines_width);
 22543       fprintf (stderr, "    %9"pD"d %9"pD"d\n",
 22544                CHARPOS (row->start.string_pos),
 22545                CHARPOS (row->end.string_pos));
 22546       fprintf (stderr, "    %9d %9d\n", row->start.dpvec_index,
 22547                row->end.dpvec_index);
 22548     }
 22549 
 22550   if (glyphs > 1)
 22551     {
 22552       int area;
 22553 
 22554       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22555         {
 22556           struct glyph *glyph = row->glyphs[area];
 22557           struct glyph *glyph_end = glyph + row->used[area];
 22558 
 22559           /* Glyph for a line end in text.  */
 22560           if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
 22561             ++glyph_end;
 22562 
 22563           if (glyph < glyph_end)
 22564             fputs (" Glyph#  Type       Pos   O   W     Code      C Face LR\n",
 22565                    stderr);
 22566 
 22567           for (; glyph < glyph_end; ++glyph)
 22568             dump_glyph (row, glyph, area);
 22569         }
 22570     }
 22571   else if (glyphs == 1)
 22572     {
 22573       int area;
 22574       char s[SHRT_MAX + 4];
 22575 
 22576       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22577         {
 22578           int i;
 22579 
 22580           for (i = 0; i < row->used[area]; ++i)
 22581             {
 22582               struct glyph *glyph = row->glyphs[area] + i;
 22583               if (i == row->used[area] - 1
 22584                   && area == TEXT_AREA
 22585                   && NILP (glyph->object)
 22586                   && glyph->type == CHAR_GLYPH
 22587                   && glyph->u.ch == ' ')
 22588                 {
 22589                   strcpy (&s[i], "[\\n]");
 22590                   i += 4;
 22591                 }
 22592               else if (glyph->type == CHAR_GLYPH
 22593                        && glyph->u.ch < 0x80
 22594                        && glyph->u.ch >= ' ')
 22595                 s[i] = glyph->u.ch;
 22596               else
 22597                 s[i] = '.';
 22598             }
 22599 
 22600           s[i] = '\0';
 22601           fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
 22602         }
 22603     }
 22604 }
 22605 
 22606 
 22607 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
 22608        Sdump_glyph_matrix, 0, 1, "p",
 22609        doc: /* Dump the current matrix of the selected window to stderr.
 22610 Shows contents of glyph row structures.  With non-nil
 22611 parameter GLYPHS, dump glyphs as well.  If GLYPHS is 1 show
 22612 glyphs in short form, otherwise show glyphs in long form.
 22613 
 22614 Interactively, no argument means show glyphs in short form;
 22615 with numeric argument, its value is passed as the GLYPHS flag.  */)
 22616   (Lisp_Object glyphs)
 22617 {
 22618   struct window *w = XWINDOW (selected_window);
 22619   struct buffer *buffer = XBUFFER (w->contents);
 22620 
 22621   fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
 22622            BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
 22623   fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
 22624            w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
 22625   fputs ("=============================================\n", stderr);
 22626   dump_glyph_matrix (w->current_matrix,
 22627                      TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 0);
 22628   return Qnil;
 22629 }
 22630 
 22631 
 22632 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
 22633        Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
 22634 Only text-mode frames have frame glyph matrices.  */)
 22635   (void)
 22636 {
 22637   struct frame *f = XFRAME (selected_frame);
 22638 
 22639   if (f->current_matrix)
 22640     dump_glyph_matrix (f->current_matrix, 1);
 22641   else
 22642     fputs ("*** This frame doesn't have a frame glyph matrix ***\n", stderr);
 22643   return Qnil;
 22644 }
 22645 
 22646 
 22647 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
 22648        doc: /* Dump glyph row ROW to stderr.
 22649 Interactively, ROW is the prefix numeric argument and defaults to
 22650 the row which displays point.
 22651 Optional argument GLYPHS 0 means don't dump glyphs.
 22652 GLYPHS 1 means dump glyphs in short form.
 22653 GLYPHS > 1 or omitted means dump glyphs in long form.  */)
 22654   (Lisp_Object row, Lisp_Object glyphs)
 22655 {
 22656   struct glyph_matrix *matrix;
 22657   EMACS_INT vpos;
 22658 
 22659   if (NILP (row))
 22660     {
 22661       int d1, d2, d3, d4, d5, ypos;
 22662       bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
 22663                                       &d1, &d2, &d3, &d4, &d5, &ypos);
 22664       if (visible_p)
 22665         vpos = ypos;
 22666       else
 22667         vpos = 0;
 22668     }
 22669   else
 22670     {
 22671       CHECK_FIXNUM (row);
 22672       vpos = XFIXNUM (row);
 22673     }
 22674   matrix = XWINDOW (selected_window)->current_matrix;
 22675   if (vpos >= 0 && vpos < matrix->nrows)
 22676     dump_glyph_row (MATRIX_ROW (matrix, vpos),
 22677                     vpos,
 22678                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22679   return Qnil;
 22680 }
 22681 
 22682 
 22683 DEFUN ("dump-tab-bar-row", Fdump_tab_bar_row, Sdump_tab_bar_row, 1, 2, "P",
 22684        doc: /* Dump glyph row ROW of the tab-bar of the current frame to stderr.
 22685 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22686 GLYPHS 0 means don't dump glyphs.
 22687 GLYPHS 1 means dump glyphs in short form.
 22688 GLYPHS > 1 or omitted means dump glyphs in long form.
 22689 
 22690 If there's no tab-bar, or if the tab-bar is not drawn by Emacs,
 22691 do nothing.  */)
 22692   (Lisp_Object row, Lisp_Object glyphs)
 22693 {
 22694 #if defined (HAVE_WINDOW_SYSTEM)
 22695   struct frame *sf = SELECTED_FRAME ();
 22696   struct glyph_matrix *m = WINDOWP (sf->tab_bar_window)
 22697     ? XWINDOW (sf->tab_bar_window)->current_matrix
 22698     : sf->current_matrix;
 22699   EMACS_INT vpos;
 22700 
 22701   if (NILP (row))
 22702     vpos = WINDOWP (sf->tab_bar_window) ? 0 :
 22703       FRAME_MENU_BAR_LINES (sf) > 0 ? 1 : 0;
 22704   else
 22705     {
 22706       CHECK_FIXNUM (row);
 22707       vpos = XFIXNUM (row);
 22708     }
 22709   if (vpos >= 0 && vpos < m->nrows)
 22710     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22711                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22712 #endif
 22713   return Qnil;
 22714 }
 22715 
 22716 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
 22717        doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
 22718 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22719 GLYPHS 0 means don't dump glyphs.
 22720 GLYPHS 1 means dump glyphs in short form.
 22721 GLYPHS > 1 or omitted means dump glyphs in long form.
 22722 
 22723 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
 22724 do nothing.  */)
 22725   (Lisp_Object row, Lisp_Object glyphs)
 22726 {
 22727 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 22728   struct frame *sf = SELECTED_FRAME ();
 22729   struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
 22730   EMACS_INT vpos;
 22731 
 22732   if (NILP (row))
 22733     vpos = 0;
 22734   else
 22735     {
 22736       CHECK_FIXNUM (row);
 22737       vpos = XFIXNUM (row);
 22738     }
 22739   if (vpos >= 0 && vpos < m->nrows)
 22740     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22741                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22742 #endif
 22743   return Qnil;
 22744 }
 22745 
 22746 
 22747 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
 22748        doc: /* Toggle tracing of redisplay.
 22749 With ARG, turn tracing on if and only if ARG is positive.  */)
 22750   (Lisp_Object arg)
 22751 {
 22752   if (NILP (arg))
 22753     trace_redisplay_p = !trace_redisplay_p;
 22754   else
 22755     {
 22756       arg = Fprefix_numeric_value (arg);
 22757       trace_redisplay_p = XFIXNUM (arg) > 0;
 22758     }
 22759 
 22760   return Qnil;
 22761 }
 22762 
 22763 
 22764 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
 22765        doc: /* Like `format', but print result to stderr.
 22766 usage: (trace-to-stderr STRING &rest OBJECTS)  */)
 22767   (ptrdiff_t nargs, Lisp_Object *args)
 22768 {
 22769   Lisp_Object s = Fformat (nargs, args);
 22770   fwrite (SDATA (s), 1, SBYTES (s), stderr);
 22771   return Qnil;
 22772 }
 22773 
 22774 #endif /* GLYPH_DEBUG */
 22775 
 22776 
 22777 
 22778 /***********************************************************************
 22779                      Building Desired Matrix Rows
 22780  ***********************************************************************/
 22781 
 22782 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
 22783    Used for non-window-redisplay windows, and for windows without left
 22784    fringe.  */
 22785 
 22786 static struct glyph_row *
 22787 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
 22788 {
 22789   struct frame *f = XFRAME (WINDOW_FRAME (w));
 22790   struct buffer *buffer = XBUFFER (w->contents);
 22791   struct buffer *old = current_buffer;
 22792   const unsigned char *arrow_string = SDATA (overlay_arrow_string);
 22793   ptrdiff_t arrow_len = SBYTES (overlay_arrow_string), char_num = 0;
 22794   const unsigned char *arrow_end = arrow_string + arrow_len;
 22795   const unsigned char *p;
 22796   struct it it;
 22797   bool multibyte_p;
 22798   int n_glyphs_before;
 22799 
 22800   set_buffer_temp (buffer);
 22801   init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
 22802   scratch_glyph_row.reversed_p = false;
 22803   it.glyph_row->used[TEXT_AREA] = 0;
 22804   SET_TEXT_POS (it.position, 0, 0);
 22805 
 22806   multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
 22807   p = arrow_string;
 22808   while (p < arrow_end)
 22809     {
 22810       Lisp_Object face, ilisp;
 22811 
 22812       /* Get the next character.  */
 22813       if (multibyte_p)
 22814         it.c = it.char_to_display = check_char_and_length (p, &it.len);
 22815       else
 22816         {
 22817           it.c = it.char_to_display = *p, it.len = 1;
 22818           if (! ASCII_CHAR_P (it.c))
 22819             it.char_to_display = BYTE8_TO_CHAR (it.c);
 22820         }
 22821       p += it.len;
 22822 
 22823       /* Get its face.  */
 22824       ilisp = make_fixnum (char_num++);
 22825       face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
 22826       it.face_id = compute_char_face (f, it.char_to_display, face);
 22827 
 22828       /* Compute its width, get its glyphs.  */
 22829       n_glyphs_before = it.glyph_row->used[TEXT_AREA];
 22830       SET_TEXT_POS (it.position, -1, -1);
 22831       PRODUCE_GLYPHS (&it);
 22832 
 22833       /* If this character doesn't fit any more in the line, we have
 22834          to remove some glyphs.  */
 22835       if (it.current_x > it.last_visible_x)
 22836         {
 22837           it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
 22838           break;
 22839         }
 22840     }
 22841 
 22842   set_buffer_temp (old);
 22843   return it.glyph_row;
 22844 }
 22845 
 22846 
 22847 /* Insert truncation glyphs at the start of IT->glyph_row.  Which
 22848    glyphs to insert is determined by produce_special_glyphs.  */
 22849 
 22850 static void
 22851 insert_left_trunc_glyphs (struct it *it)
 22852 {
 22853   struct it truncate_it;
 22854   struct glyph *from, *end, *to, *toend;
 22855 
 22856   eassert (!FRAME_WINDOW_P (it->f)
 22857            || (!it->glyph_row->reversed_p
 22858                && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 22859            || (it->glyph_row->reversed_p
 22860                && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
 22861 
 22862   /* Get the truncation glyphs.  */
 22863   truncate_it = *it;
 22864   truncate_it.current_x = 0;
 22865   truncate_it.face_id = DEFAULT_FACE_ID;
 22866   truncate_it.glyph_row = &scratch_glyph_row;
 22867   truncate_it.area = TEXT_AREA;
 22868   truncate_it.glyph_row->used[TEXT_AREA] = 0;
 22869   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
 22870   truncate_it.object = Qnil;
 22871   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
 22872 
 22873   /* Overwrite glyphs from IT with truncation glyphs.  */
 22874   if (!it->glyph_row->reversed_p)
 22875     {
 22876       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 22877 
 22878       from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22879       end = from + tused;
 22880       to = it->glyph_row->glyphs[TEXT_AREA];
 22881       toend = to + it->glyph_row->used[TEXT_AREA];
 22882       if (FRAME_WINDOW_P (it->f))
 22883         {
 22884           /* On GUI frames, when variable-size fonts are displayed,
 22885              the truncation glyphs may need more pixels than the row's
 22886              glyphs they overwrite.  We overwrite more glyphs to free
 22887              enough screen real estate, and enlarge the stretch glyph
 22888              on the right (see display_line), if there is one, to
 22889              preserve the screen position of the truncation glyphs on
 22890              the right.  */
 22891           int w = 0;
 22892           struct glyph *g = to;
 22893           short used;
 22894 
 22895           /* The first glyph could be partially visible, in which case
 22896              it->glyph_row->x will be negative.  But we want the left
 22897              truncation glyphs to be aligned at the left margin of the
 22898              window, so we override the x coordinate at which the row
 22899              will begin.  */
 22900           it->glyph_row->x = 0;
 22901           while (g < toend && w < it->truncation_pixel_width)
 22902             {
 22903               w += g->pixel_width;
 22904               ++g;
 22905             }
 22906           if (g - to - tused > 0)
 22907             {
 22908               memmove (to + tused, g, (toend - g) * sizeof(*g));
 22909               it->glyph_row->used[TEXT_AREA] -= g - to - tused;
 22910             }
 22911           used = it->glyph_row->used[TEXT_AREA];
 22912           if (it->glyph_row->truncated_on_right_p
 22913               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
 22914               && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
 22915               == STRETCH_GLYPH)
 22916             {
 22917               int extra = w - it->truncation_pixel_width;
 22918 
 22919               it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
 22920             }
 22921         }
 22922 
 22923       while (from < end)
 22924         *to++ = *from++;
 22925 
 22926       /* There may be padding glyphs left over.  Overwrite them too.  */
 22927       if (!FRAME_WINDOW_P (it->f))
 22928         {
 22929           while (to < toend && CHAR_GLYPH_PADDING_P (*to))
 22930             {
 22931               from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22932               while (from < end)
 22933                 *to++ = *from++;
 22934             }
 22935         }
 22936 
 22937       if (to > toend)
 22938         it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
 22939     }
 22940   else
 22941     {
 22942       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 22943 
 22944       /* In R2L rows, overwrite the last (rightmost) glyphs, and do
 22945          that back to front.  */
 22946       end = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22947       from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 22948       toend = it->glyph_row->glyphs[TEXT_AREA];
 22949       to = toend + it->glyph_row->used[TEXT_AREA] - 1;
 22950       if (FRAME_WINDOW_P (it->f))
 22951         {
 22952           int w = 0;
 22953           struct glyph *g = to;
 22954 
 22955           while (g >= toend && w < it->truncation_pixel_width)
 22956             {
 22957               w += g->pixel_width;
 22958               --g;
 22959             }
 22960           if (to - g - tused > 0)
 22961             to = g + tused;
 22962           if (it->glyph_row->truncated_on_right_p
 22963               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
 22964               && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
 22965             {
 22966               int extra = w - it->truncation_pixel_width;
 22967 
 22968               it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
 22969             }
 22970         }
 22971 
 22972       while (from >= end && to >= toend)
 22973         *to-- = *from--;
 22974       if (!FRAME_WINDOW_P (it->f))
 22975         {
 22976           while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
 22977             {
 22978               from =
 22979                 truncate_it.glyph_row->glyphs[TEXT_AREA]
 22980                 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 22981               while (from >= end && to >= toend)
 22982                 *to-- = *from--;
 22983             }
 22984         }
 22985       if (from >= end)
 22986         {
 22987           /* Need to free some room before prepending additional
 22988              glyphs.  */
 22989           int move_by = from - end + 1;
 22990           struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
 22991           struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
 22992 
 22993           for ( ; g >= g0; g--)
 22994             g[move_by] = *g;
 22995           while (from >= end)
 22996             *to-- = *from--;
 22997           it->glyph_row->used[TEXT_AREA] += move_by;
 22998         }
 22999     }
 23000 }
 23001 
 23002 /* Compute the hash code for ROW.  */
 23003 unsigned
 23004 row_hash (struct glyph_row *row)
 23005 {
 23006   int area, k;
 23007   unsigned hashval = 0;
 23008 
 23009   for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 23010     for (k = 0; k < row->used[area]; ++k)
 23011       hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
 23012                   + row->glyphs[area][k].u.val
 23013                   + row->glyphs[area][k].face_id
 23014                   + row->glyphs[area][k].padding_p
 23015                   + (row->glyphs[area][k].type << 2));
 23016 
 23017   return hashval;
 23018 }
 23019 
 23020 /* Compute the pixel height and width of IT->glyph_row.
 23021 
 23022    Most of the time, ascent and height of a display line will be equal
 23023    to the max_ascent and max_height values of the display iterator
 23024    structure.  This is not the case if
 23025 
 23026    1. We hit ZV without displaying anything.  In this case, max_ascent
 23027    and max_height will be zero.
 23028 
 23029    2. We have some glyphs that don't contribute to the line height.
 23030    (The glyph row flag contributes_to_line_height_p is for future
 23031    pixmap extensions).
 23032 
 23033    The first case is easily covered by using default values because in
 23034    these cases, the line height does not really matter, except that it
 23035    must not be zero.  */
 23036 
 23037 static void
 23038 compute_line_metrics (struct it *it)
 23039 {
 23040   struct glyph_row *row = it->glyph_row;
 23041 
 23042   if (FRAME_WINDOW_P (it->f))
 23043     {
 23044       int i, min_y, max_y;
 23045 
 23046       /* The line may consist of one space only, that was added to
 23047          place the cursor on it.  If so, the row's height hasn't been
 23048          computed yet.  */
 23049       if (row->height == 0)
 23050         {
 23051           if (it->max_ascent + it->max_descent == 0)
 23052             it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
 23053           row->ascent = it->max_ascent;
 23054           row->height = it->max_ascent + it->max_descent;
 23055           row->phys_ascent = it->max_phys_ascent;
 23056           row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 23057           row->extra_line_spacing = it->max_extra_line_spacing;
 23058         }
 23059 
 23060       /* Compute the width of this line.  */
 23061       row->pixel_width = row->x;
 23062       for (i = 0; i < row->used[TEXT_AREA]; ++i)
 23063         row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
 23064 
 23065       eassert (row->pixel_width >= 0);
 23066       eassert (row->ascent >= 0 && row->height > 0);
 23067 
 23068       row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
 23069                             || MATRIX_ROW_OVERLAPS_PRED_P (row));
 23070 
 23071       /* If first line's physical ascent is larger than its logical
 23072          ascent, use the physical ascent, and make the row taller.
 23073          This makes accented characters fully visible.  */
 23074       if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
 23075           && row->phys_ascent > row->ascent)
 23076         {
 23077           row->height += row->phys_ascent - row->ascent;
 23078           row->ascent = row->phys_ascent;
 23079         }
 23080 
 23081       /* Compute how much of the line is visible.  */
 23082       row->visible_height = row->height;
 23083 
 23084       min_y = WINDOW_TAB_LINE_HEIGHT (it->w) + WINDOW_HEADER_LINE_HEIGHT (it->w);
 23085       max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
 23086 
 23087       if (row->y < min_y)
 23088         row->visible_height -= min_y - row->y;
 23089       if (row->y + row->height > max_y)
 23090         row->visible_height -= row->y + row->height - max_y;
 23091     }
 23092   else
 23093     {
 23094       row->pixel_width = row->used[TEXT_AREA];
 23095       if (row->continued_p)
 23096         row->pixel_width -= it->continuation_pixel_width;
 23097       else if (row->truncated_on_right_p)
 23098         row->pixel_width -= it->truncation_pixel_width;
 23099       row->ascent = row->phys_ascent = 0;
 23100       row->height = row->phys_height = row->visible_height = 1;
 23101       row->extra_line_spacing = 0;
 23102     }
 23103 
 23104   /* Compute a hash code for this row.  */
 23105   row->hash = row_hash (row);
 23106 
 23107   it->max_ascent = it->max_descent = 0;
 23108   it->max_phys_ascent = it->max_phys_descent = 0;
 23109 }
 23110 
 23111 
 23112 static void
 23113 clear_position (struct it *it)
 23114 {
 23115   it->position.charpos = 0;
 23116   it->position.bytepos = 0;
 23117 }
 23118 
 23119 /* Append one space to the glyph row of iterator IT if doing a
 23120    window-based redisplay.  The space has the same face as
 23121    IT->face_id.  Value is true if a space was added.
 23122 
 23123    This function is called to make sure that there is always one glyph
 23124    at the end of a glyph row that the cursor can be set on under
 23125    window-systems.  (If there weren't such a glyph we would not know
 23126    how wide and tall a box cursor should be displayed).
 23127 
 23128    At the same time this space let's a nicely handle clearing to the
 23129    end of the line if the row ends in italic text.  */
 23130 
 23131 static bool
 23132 append_space_for_newline (struct it *it, bool default_face_p)
 23133 {
 23134   int n = it->glyph_row->used[TEXT_AREA];
 23135 
 23136   if (it->glyph_row->glyphs[TEXT_AREA] + n
 23137       < it->glyph_row->glyphs[1 + TEXT_AREA])
 23138     {
 23139       /* Save some values that must not be changed.
 23140          Must save IT->c and IT->len because otherwise
 23141          ITERATOR_AT_END_P wouldn't work anymore after
 23142          append_space_for_newline has been called.  */
 23143       enum display_element_type saved_what = it->what;
 23144       int saved_c = it->c, saved_len = it->len;
 23145       int saved_char_to_display = it->char_to_display;
 23146       int saved_x = it->current_x;
 23147       const int saved_face_id = it->face_id;
 23148       bool saved_box_end = it->end_of_box_run_p;
 23149       struct text_pos saved_pos = it->position;
 23150       Lisp_Object saved_object = it->object;
 23151       struct face *face;
 23152 
 23153       it->what = IT_CHARACTER;
 23154       clear_position (it);
 23155       it->object = Qnil;
 23156       it->len = 1;
 23157 
 23158       int char_width = 1;
 23159 
 23160       if (default_face_p
 23161 #ifdef HAVE_WINDOW_SYSTEM
 23162           || FRAME_WINDOW_P (it->f)
 23163 #endif
 23164           )
 23165         {
 23166           const int local_default_face_id =
 23167             lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
 23168 
 23169 #ifdef HAVE_WINDOW_SYSTEM
 23170           if (FRAME_WINDOW_P (it->f))
 23171             {
 23172               struct face *default_face
 23173                 = FACE_FROM_ID (it->f, local_default_face_id);
 23174               struct font *font = (default_face->font
 23175                                    ? default_face->font
 23176                                    : FRAME_FONT (it->f));
 23177               char_width = (font->average_width
 23178                             ? font->average_width
 23179                             : font->space_width);
 23180             }
 23181 #endif
 23182           if (default_face_p)
 23183             it->face_id = local_default_face_id;
 23184         }
 23185       /* Corner case for when display-fill-column-indicator-mode
 23186          is active and the extra character should be added in the
 23187          same place than the line.  */
 23188 
 23189       const int indicator_column =
 23190         fill_column_indicator_column (it, char_width);
 23191       int saved_end_of_box_run = it->end_of_box_run_p;
 23192       bool should_keep_end_of_box_run = false;
 23193 
 23194       if (it->current_x == indicator_column)
 23195         {
 23196           it->c = it->char_to_display
 23197             = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23198           it->face_id
 23199             = merge_faces (it->w, Qfill_column_indicator,
 23200                            0, saved_face_id);
 23201           face = FACE_FROM_ID (it->f, it->face_id);
 23202         }
 23203       else
 23204         {
 23205           it->c = it->char_to_display = ' ';
 23206           /* If the default face was remapped, be sure to use the
 23207              remapped face for the appended newline.  */
 23208 
 23209           face = FACE_FROM_ID (it->f, it->face_id);
 23210           it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
 23211           /* In R2L rows, we will prepend a stretch glyph that will
 23212              have the end_of_box_run_p flag set for it, so there's no
 23213              need for the appended newline glyph to have that flag
 23214              set.  */
 23215           if (!(it->glyph_row->reversed_p
 23216                 /* But if the appended newline glyph goes all the way to
 23217                    the end of the row, there will be no stretch glyph,
 23218                    so leave the box flag set.  */
 23219                 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x))
 23220             should_keep_end_of_box_run = true;
 23221         }
 23222       PRODUCE_GLYPHS (it);
 23223       /* Restore the end_of_box_run_p flag which was reset by
 23224          PRODUCE_GLYPHS.  */
 23225       if (should_keep_end_of_box_run)
 23226         it->end_of_box_run_p = saved_end_of_box_run;
 23227 #ifdef HAVE_WINDOW_SYSTEM
 23228       if (FRAME_WINDOW_P (it->f))
 23229         {
 23230           /* Make sure this space glyph has the right ascent and
 23231              descent values, or else cursor at end of line will look
 23232              funny, and height of empty lines will be incorrect.  */
 23233           struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
 23234           struct font *font = face->font ? face->font : FRAME_FONT (it->f);
 23235           if (n == 0)
 23236             {
 23237               Lisp_Object height, total_height;
 23238               int extra_line_spacing = it->extra_line_spacing;
 23239               int boff = font->baseline_offset;
 23240 
 23241               if (font->vertical_centering)
 23242                 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 23243 
 23244               it->object = saved_object; /* get_it_property needs this */
 23245               normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 23246               /* Must do a subset of line height processing from
 23247                  gui_produce_glyph for newline characters.  */
 23248               height = get_it_property (it, Qline_height);
 23249               if (CONSP (height)
 23250                   && CONSP (XCDR (height))
 23251                   && NILP (XCDR (XCDR (height))))
 23252                 {
 23253                   total_height = XCAR (XCDR (height));
 23254                   height = XCAR (height);
 23255                 }
 23256               else
 23257                 total_height = Qnil;
 23258               height = calc_line_height_property (it, height, font, boff, true);
 23259 
 23260               if (it->override_ascent >= 0)
 23261                 {
 23262                   it->ascent = it->override_ascent;
 23263                   it->descent = it->override_descent;
 23264                   boff = it->override_boff;
 23265                 }
 23266               if (EQ (height, Qt))
 23267                 extra_line_spacing = 0;
 23268               else
 23269                 {
 23270                   Lisp_Object spacing;
 23271 
 23272                   it->phys_ascent = it->ascent;
 23273                   it->phys_descent = it->descent;
 23274                   if (!NILP (height)
 23275                       && XFIXNUM (height) > it->ascent + it->descent)
 23276                     it->ascent = XFIXNUM (height) - it->descent;
 23277 
 23278                   if (!NILP (total_height))
 23279                     spacing = calc_line_height_property (it, total_height, font,
 23280                                                          boff, false);
 23281                   else
 23282                     {
 23283                       spacing = get_it_property (it, Qline_spacing);
 23284                       spacing = calc_line_height_property (it, spacing, font,
 23285                                                            boff, false);
 23286                     }
 23287                   if (FIXNUMP (spacing))
 23288                     {
 23289                       extra_line_spacing = XFIXNUM (spacing);
 23290                       if (!NILP (total_height))
 23291                         extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 23292                     }
 23293                 }
 23294               if (extra_line_spacing > 0)
 23295                 {
 23296                   it->descent += extra_line_spacing;
 23297                   if (extra_line_spacing > it->max_extra_line_spacing)
 23298                     it->max_extra_line_spacing = extra_line_spacing;
 23299                 }
 23300               it->max_ascent = it->ascent;
 23301               it->max_descent = it->descent;
 23302               /* Make sure compute_line_metrics recomputes the row height.  */
 23303               it->glyph_row->height = 0;
 23304             }
 23305 
 23306           g->ascent = it->max_ascent;
 23307           g->descent = it->max_descent;
 23308         }
 23309 #endif /* HAVE_WINDOW_SYSTEM  */
 23310       it->override_ascent = -1;
 23311       it->constrain_row_ascent_descent_p = false;
 23312       it->current_x = saved_x;
 23313       it->object = saved_object;
 23314       it->position = saved_pos;
 23315       it->what = saved_what;
 23316       it->face_id = saved_face_id;
 23317       it->len = saved_len;
 23318       it->c = saved_c;
 23319       it->char_to_display = saved_char_to_display;
 23320       it->end_of_box_run_p = saved_box_end;
 23321       return true;
 23322     }
 23323 
 23324   return false;
 23325 }
 23326 
 23327 
 23328 /* Extend the face of the last glyph in the text area of IT->glyph_row
 23329    to the end of the display line.  Called from display_line.  If the
 23330    glyph row is empty, add a space glyph to it so that we know the
 23331    face to draw.  Set the glyph row flag fill_line_p.  If the glyph
 23332    row is R2L, prepend a stretch glyph to cover the empty space to the
 23333    left of the leftmost glyph.  */
 23334 
 23335 static void
 23336 extend_face_to_end_of_line (struct it *it)
 23337 {
 23338   struct frame *f = it->f;
 23339 
 23340   /* If line is already filled, do nothing.  Non window-system frames
 23341      get a grace of one more ``pixel'' because their characters are
 23342      1-``pixel'' wide, so they hit the equality too early.  This grace
 23343      is needed only for R2L rows that are not continued, to produce
 23344      one extra blank where we could display the cursor.  */
 23345   if ((it->current_x >= it->last_visible_x
 23346        + (!FRAME_WINDOW_P (f)
 23347           && it->glyph_row->reversed_p
 23348           && !it->glyph_row->continued_p))
 23349       /* If the window has display margins, we will need to extend
 23350          their face even if the text area is filled.  */
 23351       && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23352            || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
 23353     return;
 23354 
 23355   specpdl_ref count = SPECPDL_INDEX ();
 23356 
 23357   /* Don't allow the user to quit out of face-merging code, in case
 23358      this is called when redisplaying a non-selected window, with
 23359      point temporarily moved to window-point.  */
 23360   specbind (Qinhibit_quit, Qt);
 23361   /* The default face, possibly remapped. */
 23362   struct face *default_face =
 23363     FACE_FROM_ID_OR_NULL (f, lookup_basic_face (it->w, f, DEFAULT_FACE_ID));
 23364   if (!default_face)
 23365     return;
 23366 
 23367   const int extend_face_id =
 23368     (it->face_id == default_face->id || it->s != NULL)
 23369     ? it->face_id
 23370     : (it->glyph_row->ends_at_zv_p
 23371        ? default_face->id
 23372        : face_at_pos (it, LFACE_EXTEND_INDEX));
 23373   unbind_to (count, Qnil);
 23374 
 23375   /* Face extension extends the background and box of IT->extend_face_id
 23376      to the end of the line.  If the background equals the background
 23377      of the frame, we don't have to do anything.  */
 23378   struct face *face = FACE_FROM_ID (f, (it->face_before_selective_p
 23379                                         ? it->saved_face_id
 23380                                         : extend_face_id));
 23381 
 23382   if (FRAME_WINDOW_P (f)
 23383       && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
 23384       && face->box == FACE_NO_BOX
 23385       && face->underline == FACE_NO_UNDERLINE
 23386       && !face->overline_p
 23387       && !face->strike_through_p
 23388       && face->background == FRAME_BACKGROUND_PIXEL (f)
 23389 #ifdef HAVE_WINDOW_SYSTEM
 23390       && !face->stipple
 23391 #endif
 23392       && !it->glyph_row->reversed_p
 23393       && !display_fill_column_indicator)
 23394     return;
 23395 
 23396   /* Set the glyph row flag indicating that the face of the last glyph
 23397      in the text area has to be drawn to the end of the text area.  */
 23398   it->glyph_row->fill_line_p = true;
 23399 
 23400   const int orig_face_id = it->face_id;
 23401   /* If current character of IT is not ASCII, make sure we have the
 23402      ASCII face.  This will be automatically undone the next time
 23403      get_next_display_element returns a multibyte character.  Note
 23404      that the character will always be single byte in unibyte
 23405      text.  */
 23406   if (!ASCII_CHAR_P (it->c))
 23407     it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
 23408 
 23409 
 23410 #ifdef HAVE_WINDOW_SYSTEM
 23411   if (FRAME_WINDOW_P (f))
 23412     {
 23413       /* If the row is empty, add a space with the current face of IT,
 23414          so that we know which face to draw.  */
 23415       if (it->glyph_row->used[TEXT_AREA] == 0)
 23416         {
 23417           it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
 23418           it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
 23419           it->glyph_row->used[TEXT_AREA] = 1;
 23420         }
 23421       /* Mode line and the header line don't have margins, and
 23422          likewise the frame's tool-bar window, if there is any.  */
 23423       if (!(it->glyph_row->mode_line_p
 23424             || (WINDOWP (f->tab_bar_window)
 23425                 && it->w == XWINDOW (f->tab_bar_window))
 23426 #ifndef HAVE_EXT_TOOL_BAR
 23427             || (WINDOWP (f->tool_bar_window)
 23428                 && it->w == XWINDOW (f->tool_bar_window))
 23429 #endif
 23430             ))
 23431         {
 23432           if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23433               && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
 23434             {
 23435               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
 23436               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
 23437                 default_face->id;
 23438               it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
 23439             }
 23440           if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23441               && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
 23442             {
 23443               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
 23444               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
 23445                 default_face->id;
 23446               it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
 23447             }
 23448 
 23449           struct font *font = (default_face->font
 23450                                ? default_face->font
 23451                                : FRAME_FONT (f));
 23452 
 23453           const int char_width = (font->average_width
 23454                                   ? font->average_width
 23455                                   : font->space_width);
 23456 
 23457           const int indicator_column =
 23458             fill_column_indicator_column (it, char_width);
 23459 
 23460           const char saved_char = it->char_to_display;
 23461           const struct text_pos saved_pos = it->position;
 23462           const bool saved_avoid_cursor = it->avoid_cursor_p;
 23463           const bool saved_box_start = it->start_of_box_run_p;
 23464           Lisp_Object save_object = it->object;
 23465           const int saved_face_id = it->face_id;
 23466 
 23467           it->face_id = extend_face_id;
 23468           it->avoid_cursor_p = true;
 23469           it->object = Qnil;
 23470 
 23471           const int stretch_height = it->ascent + it->descent;
 23472           const int stretch_ascent =
 23473             (stretch_height * FONT_BASE (font)) / FONT_HEIGHT (font);
 23474 
 23475           if (indicator_column >= 0
 23476               && indicator_column > it->current_x
 23477               && indicator_column < it->last_visible_x)
 23478             {
 23479 
 23480               /* Here we subtract char_width because we want the
 23481                  column indicator in the column INDICATOR_COLUMN,
 23482                  not after it.  */
 23483               const int stretch_width =
 23484                 indicator_column - it->current_x - char_width;
 23485 
 23486               clear_position (it);
 23487 
 23488               /* Only generate a stretch glyph if there is distance
 23489                  between current_x and the indicator position.  */
 23490               if (stretch_width > 0)
 23491                 {
 23492                   append_stretch_glyph (it, Qnil, stretch_width,
 23493                                         stretch_height, stretch_ascent);
 23494                 }
 23495 
 23496               /* Generate the glyph indicator only if
 23497                  append_space_for_newline didn't already.  */
 23498               if (it->current_x < indicator_column)
 23499                 {
 23500                   const int save_face_id = it->face_id;
 23501                   const int save_ascent = it->ascent;
 23502                   const int save_descent = it->descent;
 23503                   it->char_to_display
 23504                     = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23505                   it->face_id
 23506                     = merge_faces (it->w, Qfill_column_indicator,
 23507                                    0, extend_face_id);
 23508                   PRODUCE_GLYPHS (it);
 23509                   it->face_id = save_face_id;
 23510                   it->ascent = save_ascent;
 23511                   it->descent = save_descent;
 23512                 }
 23513             }
 23514 
 23515           /* Fill space until window edge with the merged face.  Do that
 23516              only for L2R rows, as R2L rows are handled specially below.  */
 23517           if (!it->glyph_row->reversed_p)
 23518             {
 23519               const int stretch_width = it->last_visible_x - it->current_x;
 23520 
 23521               if (stretch_width > 0)
 23522                 {
 23523                   clear_position (it);
 23524                   append_stretch_glyph (it, Qnil, stretch_width,
 23525                                         stretch_height, stretch_ascent);
 23526                 }
 23527             }
 23528 
 23529           it->char_to_display = saved_char;
 23530           it->position = saved_pos;
 23531           it->avoid_cursor_p = saved_avoid_cursor;
 23532           it->start_of_box_run_p = saved_box_start;
 23533           it->object = save_object;
 23534           it->face_id = saved_face_id;
 23535         }
 23536       if (it->glyph_row->reversed_p)
 23537         {
 23538           /* Prepend a stretch glyph to the row, such that the
 23539              rightmost glyph will be drawn flushed all the way to the
 23540              right margin of the window.  The stretch glyph that will
 23541              occupy the empty space, if any, to the left of the
 23542              glyph.  */
 23543           struct font *font = face->font ? face->font : FRAME_FONT (f);
 23544           struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
 23545           struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
 23546           struct glyph *g;
 23547           int row_width, stretch_ascent, stretch_width;
 23548           struct text_pos saved_pos;
 23549           int saved_face_id;
 23550           bool saved_avoid_cursor, saved_box_start;
 23551 
 23552           for (row_width = 0, g = row_start; g < row_end; g++)
 23553             row_width += g->pixel_width;
 23554 
 23555           /* FIXME: There are various minor display glitches in R2L
 23556              rows when only one of the fringes is missing.  The
 23557              strange condition below produces the least bad effect.  */
 23558           if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23559               == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
 23560               || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
 23561             stretch_width = window_box_width (it->w, TEXT_AREA);
 23562           else
 23563             stretch_width = it->last_visible_x - it->first_visible_x;
 23564           stretch_width -= row_width;
 23565 
 23566           if (stretch_width > 0)
 23567             {
 23568               stretch_ascent =
 23569                 (((it->ascent + it->descent)
 23570                   * FONT_BASE (font)) / FONT_HEIGHT (font));
 23571               saved_pos = it->position;
 23572               clear_position (it);
 23573               saved_avoid_cursor = it->avoid_cursor_p;
 23574               it->avoid_cursor_p = true;
 23575               saved_face_id = it->face_id;
 23576               saved_box_start = it->start_of_box_run_p;
 23577               /* The last row's stretch glyph should get the default
 23578                  face, to avoid painting the rest of the window with
 23579                  the region face, if the region ends at ZV.  */
 23580               it->face_id = (it->glyph_row->ends_at_zv_p ?
 23581                              default_face->id : face->id);
 23582 
 23583               it->start_of_box_run_p = false;
 23584               append_stretch_glyph (it, Qnil, stretch_width,
 23585                                     it->ascent + it->descent, stretch_ascent);
 23586               it->position = saved_pos;
 23587               it->avoid_cursor_p = saved_avoid_cursor;
 23588               it->face_id = saved_face_id;
 23589               it->start_of_box_run_p = saved_box_start;
 23590             }
 23591           /* If stretch_width comes out negative, it means that the
 23592              last glyph is only partially visible.  In R2L rows, we
 23593              want the leftmost glyph to be partially visible, so we
 23594              need to give the row the corresponding left offset.  */
 23595           if (stretch_width < 0)
 23596             it->glyph_row->x = stretch_width;
 23597         }
 23598       it->face_id = orig_face_id;
 23599     }
 23600   else
 23601 #endif  /* HAVE_WINDOW_SYSTEM */
 23602     {
 23603       /* Save some values that must not be changed.  */
 23604       int saved_x = it->current_x;
 23605       struct text_pos saved_pos = it->position;
 23606       Lisp_Object saved_object = it->object;;
 23607       enum display_element_type saved_what = it->what;
 23608 
 23609       it->what = IT_CHARACTER;
 23610       clear_position (it);
 23611       it->object = Qnil;
 23612       it->c = it->char_to_display = ' ';
 23613       it->len = 1;
 23614 
 23615       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23616           && (it->glyph_row->used[LEFT_MARGIN_AREA]
 23617               < WINDOW_LEFT_MARGIN_WIDTH (it->w))
 23618           && !it->glyph_row->mode_line_p
 23619           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23620         {
 23621           struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
 23622           struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
 23623 
 23624           for (it->current_x = 0; g < e; g++)
 23625             it->current_x += g->pixel_width;
 23626 
 23627           it->area = LEFT_MARGIN_AREA;
 23628           it->face_id = default_face->id;
 23629           while (it->glyph_row->used[LEFT_MARGIN_AREA]
 23630                  < WINDOW_LEFT_MARGIN_WIDTH (it->w)
 23631                  && g < it->glyph_row->glyphs[TEXT_AREA])
 23632             {
 23633               PRODUCE_GLYPHS (it);
 23634               /* term.c:produce_glyphs advances it->current_x only for
 23635                  TEXT_AREA.  */
 23636               it->current_x += it->pixel_width;
 23637               g++;
 23638             }
 23639 
 23640           it->current_x = saved_x;
 23641           it->area = TEXT_AREA;
 23642         }
 23643 
 23644       /* The last row's blank glyphs should get the default face, to
 23645          avoid painting the rest of the window with the region face,
 23646          if the region ends at ZV.  */
 23647       it->face_id = (it->glyph_row->ends_at_zv_p ?
 23648                      default_face->id : face->id);
 23649 
 23650       /* Make sure our idea of current_x is in sync with the glyphs
 23651          actually in the glyph row.  They might differ because
 23652          append_space_for_newline can insert one glyph without
 23653          updating current_x.  */
 23654       it->current_x = it->glyph_row->used[TEXT_AREA];
 23655 
 23656       /* The above assignment causes the code below to use a
 23657          non-standard semantics of it->current_x: it is measured
 23658          relative to the beginning of the text-area, thus disregarding
 23659          the window's hscroll.  That is why we need to correct the
 23660          indicator column for the hscroll, otherwise the indicator
 23661          will not move together with the text as result of horizontal
 23662          scrolling.  */
 23663       const int indicator_column =
 23664         fill_column_indicator_column (it, 1) - it->first_visible_x;
 23665 
 23666       /* Display fill-column indicator if needed.  */
 23667       while (it->current_x <= it->last_visible_x)
 23668         {
 23669           if (it->current_x != indicator_column)
 23670             PRODUCE_GLYPHS (it);
 23671           else
 23672             {
 23673               int saved_face_id = it->face_id;
 23674               it->face_id
 23675                 = merge_faces (it->w, Qfill_column_indicator, 0, extend_face_id);
 23676               it->c = it->char_to_display
 23677                 = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23678 
 23679               PRODUCE_GLYPHS (it);
 23680 
 23681               it->face_id = saved_face_id;
 23682               it->c = it->char_to_display = ' ';
 23683             }
 23684         }
 23685 
 23686       if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23687           && (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23688               < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
 23689           && !it->glyph_row->mode_line_p
 23690           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23691         {
 23692           struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
 23693           struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
 23694 
 23695           for ( ; g < e; g++)
 23696             it->current_x += g->pixel_width;
 23697 
 23698           it->area = RIGHT_MARGIN_AREA;
 23699           it->face_id = default_face->id;
 23700           while (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23701                  < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
 23702                  && g < it->glyph_row->glyphs[LAST_AREA])
 23703             {
 23704               PRODUCE_GLYPHS (it);
 23705               it->current_x += it->pixel_width;
 23706               g++;
 23707             }
 23708 
 23709           it->area = TEXT_AREA;
 23710         }
 23711 
 23712       /* Don't count these blanks really.  It would let us insert a left
 23713          truncation glyph below and make us set the cursor on them, maybe.  */
 23714       it->current_x = saved_x;
 23715       it->object = saved_object;
 23716       it->position = saved_pos;
 23717       it->what = saved_what;
 23718       it->face_id = orig_face_id;
 23719     }
 23720 }
 23721 
 23722 
 23723 /* Value is true if text starting at CHARPOS in current_buffer is
 23724    trailing whitespace.  */
 23725 
 23726 static bool
 23727 trailing_whitespace_p (ptrdiff_t charpos)
 23728 {
 23729   ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
 23730   int c = 0;
 23731 
 23732   while (bytepos < ZV_BYTE
 23733          && (c = FETCH_BYTE (bytepos),
 23734              c == ' ' || c == '\t'))
 23735     ++bytepos;
 23736 
 23737   if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
 23738     {
 23739       if (bytepos != PT_BYTE)
 23740         return true;
 23741     }
 23742   return false;
 23743 }
 23744 
 23745 
 23746 /* Highlight trailing whitespace, if any, in row at IT.  */
 23747 
 23748 static void
 23749 highlight_trailing_whitespace (struct it *it)
 23750 {
 23751   struct glyph_row *row = it->glyph_row;
 23752   int used = row->used[TEXT_AREA];
 23753 
 23754   if (used)
 23755     {
 23756       struct glyph *start = row->glyphs[TEXT_AREA];
 23757       struct glyph *glyph = start + used - 1;
 23758 
 23759       if (row->reversed_p)
 23760         {
 23761           /* Right-to-left rows need to be processed in the opposite
 23762              direction, so swap the edge pointers. */
 23763           glyph = start;
 23764           start = row->glyphs[TEXT_AREA] + used - 1;
 23765         }
 23766 
 23767       /* Skip over glyphs inserted to display the cursor at the
 23768          end of a line, for extending the face of the last glyph
 23769          to the end of the line on terminals, and for truncation
 23770          and continuation glyphs.  */
 23771       if (!row->reversed_p)
 23772         {
 23773           while (glyph >= start
 23774                  && (glyph->type == CHAR_GLYPH
 23775                      || glyph->type == STRETCH_GLYPH)
 23776                  && NILP (glyph->object))
 23777             --glyph;
 23778         }
 23779       else
 23780         {
 23781           while (glyph <= start
 23782                  && (glyph->type == CHAR_GLYPH || glyph->type == STRETCH_GLYPH)
 23783                  && NILP (glyph->object))
 23784             ++glyph;
 23785         }
 23786 
 23787       /* If last glyph is a space or stretch, and it's trailing
 23788          whitespace, set the face of all trailing whitespace glyphs in
 23789          IT->glyph_row to `trailing-whitespace'.  */
 23790       if ((row->reversed_p ? glyph <= start : glyph >= start)
 23791           && BUFFERP (glyph->object)
 23792           && (glyph->type == STRETCH_GLYPH
 23793               || (glyph->type == CHAR_GLYPH
 23794                   && glyph->u.ch == ' '))
 23795           && trailing_whitespace_p (glyph->charpos))
 23796         {
 23797           int face_id = lookup_named_face (it->w, it->f, Qtrailing_whitespace, false);
 23798           if (face_id < 0)
 23799             return;
 23800 
 23801           if (!row->reversed_p)
 23802             {
 23803               while (glyph >= start
 23804                      && BUFFERP (glyph->object)
 23805                      && (glyph->type == STRETCH_GLYPH
 23806                          || (glyph->type == CHAR_GLYPH
 23807                              && glyph->u.ch == ' ')))
 23808                 (glyph--)->face_id = face_id;
 23809             }
 23810           else
 23811             {
 23812               while (glyph <= start
 23813                      && BUFFERP (glyph->object)
 23814                      && (glyph->type == STRETCH_GLYPH
 23815                          || (glyph->type == CHAR_GLYPH
 23816                              && glyph->u.ch == ' ')))
 23817                 (glyph++)->face_id = face_id;
 23818             }
 23819         }
 23820     }
 23821 }
 23822 
 23823 
 23824 /* Value is true if glyph row ROW should be
 23825    considered to hold the buffer position CHARPOS.  */
 23826 
 23827 static bool
 23828 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
 23829 {
 23830   bool result = true;
 23831 
 23832   if (charpos == CHARPOS (row->end.pos)
 23833       || charpos == MATRIX_ROW_END_CHARPOS (row))
 23834     {
 23835       /* Suppose the row ends on a string.
 23836          Unless the row is continued, that means it ends on a newline
 23837          in the string.  If it's anything other than a display string
 23838          (e.g., a before-string from an overlay), we don't want the
 23839          cursor there.  (This heuristic seems to give the optimal
 23840          behavior for the various types of multi-line strings.)
 23841          One exception: if the string has `cursor' property on one of
 23842          its characters, we _do_ want the cursor there.  */
 23843       if (CHARPOS (row->end.string_pos) >= 0)
 23844         {
 23845           if (row->continued_p)
 23846             result = true;
 23847           else
 23848             {
 23849               /* Check for `display' property.  */
 23850               struct glyph *beg = row->glyphs[TEXT_AREA];
 23851               struct glyph *end = beg + row->used[TEXT_AREA] - 1;
 23852               struct glyph *glyph;
 23853 
 23854               result = false;
 23855               for (glyph = end; glyph >= beg; --glyph)
 23856                 if (STRINGP (glyph->object))
 23857                   {
 23858                     Lisp_Object prop
 23859                       = Fget_char_property (make_fixnum (charpos),
 23860                                             Qdisplay, Qnil);
 23861                     result =
 23862                       (!NILP (prop)
 23863                        && display_prop_string_p (prop, glyph->object));
 23864                     /* If there's a `cursor' property on one of the
 23865                        string's characters, this row is a cursor row,
 23866                        even though this is not a display string.  */
 23867                     if (!result)
 23868                       {
 23869                         Lisp_Object s = glyph->object;
 23870 
 23871                         for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
 23872                           {
 23873                             ptrdiff_t gpos = glyph->charpos;
 23874 
 23875                             if (!NILP (Fget_char_property (make_fixnum (gpos),
 23876                                                            Qcursor, s)))
 23877                               {
 23878                                 result = true;
 23879                                 break;
 23880                               }
 23881                           }
 23882                       }
 23883                     break;
 23884                   }
 23885             }
 23886         }
 23887       else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 23888         {
 23889           /* If the row ends in middle of a real character,
 23890              and the line is continued, we want the cursor here.
 23891              That's because CHARPOS (ROW->end.pos) would equal
 23892              PT if PT is before the character.  */
 23893           if (!row->ends_in_ellipsis_p)
 23894             result = row->continued_p;
 23895           else
 23896           /* If the row ends in an ellipsis, then
 23897              CHARPOS (ROW->end.pos) will equal point after the
 23898              invisible text.  We want that position to be displayed
 23899              after the ellipsis.  */
 23900             result = false;
 23901         }
 23902       /* If the row ends at ZV, display the cursor at the end of that
 23903          row instead of at the start of the row below.  */
 23904       else
 23905         result = row->ends_at_zv_p;
 23906     }
 23907 
 23908   return result;
 23909 }
 23910 
 23911 /* Value is true if glyph row ROW should be
 23912    used to hold the cursor.  */
 23913 
 23914 static bool
 23915 cursor_row_p (struct glyph_row *row)
 23916 {
 23917   return row_for_charpos_p (row, PT);
 23918 }
 23919 
 23920 
 23921 
 23922 /* Push the property PROP so that it will be rendered at the current
 23923    position in IT.  Return true if PROP was successfully pushed, false
 23924    otherwise.  Called from handle_line_prefix to handle the
 23925    `line-prefix' and `wrap-prefix' properties.  */
 23926 
 23927 static bool
 23928 push_prefix_prop (struct it *it, Lisp_Object prop)
 23929 {
 23930   struct text_pos pos =
 23931     STRINGP (it->string) ? it->current.string_pos : it->current.pos;
 23932 
 23933   eassert (it->method == GET_FROM_BUFFER
 23934            || it->method == GET_FROM_DISPLAY_VECTOR
 23935            || it->method == GET_FROM_STRING
 23936            || it->method == GET_FROM_IMAGE);
 23937 
 23938   /* We need to save the current buffer/string position, so it will be
 23939      restored by pop_it, because iterate_out_of_display_property
 23940      depends on that being set correctly, but some situations leave
 23941      it->position not yet set when this function is called.  */
 23942   push_it (it, &pos);
 23943 
 23944   if (STRINGP (prop))
 23945     {
 23946       if (SCHARS (prop) == 0)
 23947         {
 23948           pop_it (it);
 23949           return false;
 23950         }
 23951 
 23952       it->string = prop;
 23953       it->string_from_prefix_prop_p = true;
 23954       it->multibyte_p = STRING_MULTIBYTE (it->string);
 23955       it->current.overlay_string_index = -1;
 23956       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
 23957       it->end_charpos = it->string_nchars = SCHARS (it->string);
 23958       it->method = GET_FROM_STRING;
 23959       it->stop_charpos = 0;
 23960       it->prev_stop = 0;
 23961       it->base_level_stop = 0;
 23962       it->cmp_it.id = -1;
 23963 
 23964       /* Force paragraph direction to be that of the parent
 23965          buffer/string.  */
 23966       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 23967         it->paragraph_embedding = it->bidi_it.paragraph_dir;
 23968       else
 23969         it->paragraph_embedding = L2R;
 23970 
 23971       /* Set up the bidi iterator for this display string.  */
 23972       if (it->bidi_p)
 23973         {
 23974           it->bidi_it.string.lstring = it->string;
 23975           it->bidi_it.string.s = NULL;
 23976           it->bidi_it.string.schars = it->end_charpos;
 23977           it->bidi_it.string.bufpos = IT_CHARPOS (*it);
 23978           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
 23979           it->bidi_it.string.unibyte = !it->multibyte_p;
 23980           it->bidi_it.w = it->w;
 23981           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
 23982         }
 23983     }
 23984   else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
 23985     {
 23986       it->method = GET_FROM_STRETCH;
 23987       it->object = prop;
 23988     }
 23989 #ifdef HAVE_WINDOW_SYSTEM
 23990   else if (IMAGEP (prop))
 23991     {
 23992       it->what = IT_IMAGE;
 23993       it->image_id = lookup_image (it->f, prop, it->face_id);
 23994       it->method = GET_FROM_IMAGE;
 23995     }
 23996 #endif /* HAVE_WINDOW_SYSTEM */
 23997   else
 23998     {
 23999       pop_it (it);              /* bogus display property, give up */
 24000       return false;
 24001     }
 24002 
 24003   return true;
 24004 }
 24005 
 24006 /* Return the character-property PROP at the current position in IT.  */
 24007 
 24008 static Lisp_Object
 24009 get_it_property (struct it *it, Lisp_Object prop)
 24010 {
 24011   Lisp_Object position, object = it->object;
 24012 
 24013   if (STRINGP (object))
 24014     position = make_fixnum (IT_STRING_CHARPOS (*it));
 24015   else if (BUFFERP (object))
 24016     {
 24017       position = make_fixnum (IT_CHARPOS (*it));
 24018       object = it->window;
 24019     }
 24020   else
 24021     return Qnil;
 24022 
 24023   return Fget_char_property (position, prop, object);
 24024 }
 24025 
 24026 /* Return the line-prefix/wrap-prefix property, checking both the
 24027    current IT->OBJECT and the underlying buffer text.  */
 24028 
 24029 static Lisp_Object
 24030 get_line_prefix_it_property (struct it *it, Lisp_Object prop)
 24031 {
 24032   Lisp_Object prefix = get_it_property (it, prop);
 24033 
 24034   /* If we are looking at a display or overlay string, check also the
 24035      underlying buffer text.  */
 24036   if (NILP (prefix) && it->sp > 0 && STRINGP (it->object))
 24037     return Fget_char_property (make_fixnum (IT_CHARPOS (*it)), prop,
 24038                                it->w->contents);
 24039   return prefix;
 24040 }
 24041 
 24042 /* See if there's a line- or wrap-prefix, and if so, push it on IT.  */
 24043 
 24044 static void
 24045 handle_line_prefix (struct it *it)
 24046 {
 24047   Lisp_Object prefix;
 24048 
 24049   if (it->continuation_lines_width > 0)
 24050     {
 24051       prefix = get_line_prefix_it_property (it, Qwrap_prefix);
 24052       if (NILP (prefix))
 24053         prefix = Vwrap_prefix;
 24054     }
 24055   else
 24056     {
 24057       prefix = get_line_prefix_it_property (it, Qline_prefix);
 24058       if (NILP (prefix))
 24059         prefix = Vline_prefix;
 24060     }
 24061   if (! NILP (prefix) && push_prefix_prop (it, prefix))
 24062     {
 24063       /* If the prefix is wider than the window, and we try to wrap
 24064          it, it would acquire its own wrap prefix, and so on till the
 24065          iterator stack overflows.  So, don't wrap the prefix.  */
 24066       it->line_wrap = TRUNCATE;
 24067       it->avoid_cursor_p = true;
 24068     }
 24069 }
 24070 
 24071 
 24072 
 24073 /* Remove N glyphs at the start of a reversed IT->glyph_row.  Called
 24074    only for R2L lines from display_line and display_string, when they
 24075    decide that too many glyphs were produced by PRODUCE_GLYPHS, and
 24076    the line/string needs to be continued on the next glyph row.  */
 24077 static void
 24078 unproduce_glyphs (struct it *it, int n)
 24079 {
 24080   struct glyph *glyph, *end;
 24081 
 24082   eassert (it->glyph_row);
 24083   eassert (it->glyph_row->reversed_p);
 24084   eassert (it->area == TEXT_AREA);
 24085   eassert (n <= it->glyph_row->used[TEXT_AREA]);
 24086 
 24087   if (n > it->glyph_row->used[TEXT_AREA])
 24088     n = it->glyph_row->used[TEXT_AREA];
 24089   glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
 24090   end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
 24091   for ( ; glyph < end; glyph++)
 24092     glyph[-n] = *glyph;
 24093 }
 24094 
 24095 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
 24096    and ROW->maxpos.  */
 24097 static void
 24098 find_row_edges (struct it *it, struct glyph_row *row,
 24099                 ptrdiff_t min_pos, ptrdiff_t min_bpos,
 24100                 ptrdiff_t max_pos, ptrdiff_t max_bpos)
 24101 {
 24102   /* FIXME: Revisit this when glyph ``spilling'' in continuation
 24103      lines' rows is implemented for bidi-reordered rows.  */
 24104 
 24105   /* ROW->minpos is the value of min_pos, the minimal buffer position
 24106      we have in ROW, or ROW->start.pos if that is smaller.  */
 24107   if (min_pos <= ZV && min_pos < row->start.pos.charpos)
 24108     SET_TEXT_POS (row->minpos, min_pos, min_bpos);
 24109   else
 24110     /* We didn't find buffer positions smaller than ROW->start, or
 24111        didn't find _any_ valid buffer positions in any of the glyphs,
 24112        so we must trust the iterator's computed positions.  */
 24113       row->minpos = row->start.pos;
 24114   if (max_pos <= 0)
 24115     {
 24116       max_pos = CHARPOS (it->current.pos);
 24117       max_bpos = BYTEPOS (it->current.pos);
 24118     }
 24119 
 24120   /* Here are the various use-cases for ending the row, and the
 24121      corresponding values for ROW->maxpos:
 24122 
 24123      Line ends in a newline from buffer       eol_pos + 1
 24124      Line is continued from buffer            max_pos + 1
 24125      Line is truncated on right               it->current.pos
 24126      Line ends in a newline from string       max_pos + 1(*)
 24127       (*) + 1 only when line ends in a forward scan
 24128      Line is continued from string            max_pos
 24129      Line is continued from display vector    max_pos
 24130      Line is entirely from a string           min_pos == max_pos
 24131      Line is entirely from a display vector   min_pos == max_pos
 24132      Line that ends at ZV                     ZV
 24133 
 24134      If you discover other use-cases, please add them here as
 24135      appropriate.  */
 24136   if (row->ends_at_zv_p)
 24137     row->maxpos = it->current.pos;
 24138   else if (row->used[TEXT_AREA])
 24139     {
 24140       bool seen_this_string = false;
 24141       struct glyph_row *r1 = row - 1;
 24142 
 24143       /* Did we see the same display string on the previous row?  */
 24144       if (STRINGP (it->object)
 24145           /* this is not the first row */
 24146           && row > it->w->desired_matrix->rows
 24147           /* previous row is not the header line or tab-line */
 24148           && !r1->mode_line_p
 24149           /* previous row also ends in a newline from a string */
 24150           && r1->ends_in_newline_from_string_p)
 24151         {
 24152           struct glyph *start, *end;
 24153 
 24154           /* Search for the last glyph of the previous row that came
 24155              from buffer or string.  Depending on whether the row is
 24156              L2R or R2L, we need to process it front to back or the
 24157              other way round.  */
 24158           if (!r1->reversed_p)
 24159             {
 24160               start = r1->glyphs[TEXT_AREA];
 24161               end = start + r1->used[TEXT_AREA];
 24162               /* Glyphs inserted by redisplay have nil as their object.  */
 24163               while (end > start
 24164                      && NILP ((end - 1)->object)
 24165                      && (end - 1)->charpos <= 0)
 24166                 --end;
 24167               if (end > start)
 24168                 {
 24169                   if (EQ ((end - 1)->object, it->object))
 24170                     seen_this_string = true;
 24171                 }
 24172               else
 24173                 /* If all the glyphs of the previous row were inserted
 24174                    by redisplay, it means the previous row was
 24175                    produced from a single newline, which is only
 24176                    possible if that newline came from the same string
 24177                    as the one which produced this ROW.  */
 24178                 seen_this_string = true;
 24179             }
 24180           else
 24181             {
 24182               end = r1->glyphs[TEXT_AREA] - 1;
 24183               start = end + r1->used[TEXT_AREA];
 24184               while (end < start
 24185                      && NILP ((end + 1)->object)
 24186                      && (end + 1)->charpos <= 0)
 24187                 ++end;
 24188               if (end < start)
 24189                 {
 24190                   if (EQ ((end + 1)->object, it->object))
 24191                     seen_this_string = true;
 24192                 }
 24193               else
 24194                 seen_this_string = true;
 24195             }
 24196         }
 24197       /* Take note of each display string that covers a newline only
 24198          once, the first time we see it.  This is for when a display
 24199          string includes more than one newline in it.  */
 24200       if (row->ends_in_newline_from_string_p && !seen_this_string)
 24201         {
 24202           /* If we were scanning the buffer forward when we displayed
 24203              the string, we want to account for at least one buffer
 24204              position that belongs to this row (position covered by
 24205              the display string), so that cursor positioning will
 24206              consider this row as a candidate when point is at the end
 24207              of the visual line represented by this row.  This is not
 24208              required when scanning back, because max_pos will already
 24209              have a much larger value.  */
 24210           if (CHARPOS (row->end.pos) > max_pos)
 24211             inc_both (&max_pos, &max_bpos);
 24212           SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24213         }
 24214       else if (CHARPOS (it->eol_pos) > 0)
 24215         SET_TEXT_POS (row->maxpos,
 24216                       CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
 24217       else if (row->continued_p)
 24218         {
 24219           /* If max_pos is different from IT's current position, it
 24220              means IT->method does not belong to the display element
 24221              at max_pos.  However, it also means that the display
 24222              element at max_pos was displayed in its entirety on this
 24223              line, which is equivalent to saying that the next line
 24224              starts at the next buffer position.  */
 24225           if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
 24226             SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24227           else
 24228             {
 24229               inc_both (&max_pos, &max_bpos);
 24230               SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24231             }
 24232         }
 24233       else if (row->truncated_on_right_p)
 24234         /* display_line already called reseat_at_next_visible_line_start,
 24235            which puts the iterator at the beginning of the next line, in
 24236            the logical order. */
 24237         row->maxpos = it->current.pos;
 24238       else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
 24239         /* A line that is entirely from a string/image/stretch...  */
 24240         row->maxpos = row->minpos;
 24241       else
 24242         emacs_abort ();
 24243     }
 24244   else
 24245     row->maxpos = it->current.pos;
 24246 }
 24247 
 24248 /* Like display_count_lines, but capable of counting outside of the
 24249    current narrowed region.  */
 24250 static ptrdiff_t
 24251 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
 24252                                ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
 24253 {
 24254   if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
 24255     return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24256 
 24257   ptrdiff_t val;
 24258   specpdl_ref pdl_count = SPECPDL_INDEX ();
 24259   record_unwind_protect (save_restriction_restore, save_restriction_save ());
 24260   labeled_restrictions_remove_in_current_buffer ();
 24261   Fwiden ();
 24262   val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24263   unbind_to (pdl_count, Qnil);
 24264   return val;
 24265 }
 24266 
 24267 /* Count the number of screen lines in window IT->w between character
 24268    position IT_CHARPOS(*IT) and the line showing that window's point.  */
 24269 static ptrdiff_t
 24270 display_count_lines_visually (struct it *it)
 24271 {
 24272   struct it tem_it;
 24273   ptrdiff_t to;
 24274   struct text_pos from;
 24275 
 24276   /* If we already calculated a relative line number, use that.  This
 24277      trick relies on the fact that visual lines (a.k.a. "glyph rows")
 24278      are laid out sequentially, one by one, for each sequence of calls
 24279      to display_line or other similar function that follows a call to
 24280      init_iterator.  */
 24281   if (it->lnum_bytepos > 0)
 24282     return it->lnum + 1;
 24283   else
 24284     {
 24285       specpdl_ref count = SPECPDL_INDEX ();
 24286 
 24287       if (IT_CHARPOS (*it) <= PT)
 24288         {
 24289           from = it->current.pos;
 24290           to = PT;
 24291         }
 24292       else
 24293         {
 24294           SET_TEXT_POS (from, PT, PT_BYTE);
 24295           to = IT_CHARPOS (*it);
 24296         }
 24297       /* Need to disable visual mode temporarily, since otherwise the
 24298          call to move_it_to below and inside start_display will cause
 24299          infinite recursion.  */
 24300       specbind (Qdisplay_line_numbers, Qrelative);
 24301       start_display (&tem_it, it->w, from);
 24302       /* Some redisplay optimizations could invoke us very far from
 24303          PT, which will make the caller painfully slow.  There should
 24304          be no need to go too far beyond the window's bottom, as any
 24305          such optimization will fail to show point anyway.  */
 24306       move_it_to (&tem_it, to, -1,
 24307                   tem_it.last_visible_y
 24308                   + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
 24309                   -1, MOVE_TO_POS | MOVE_TO_Y);
 24310       unbind_to (count, Qnil);
 24311       return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
 24312     }
 24313 }
 24314 
 24315 /* Produce the line-number glyphs for the current glyph_row.  If
 24316    IT->glyph_row is non-NULL, populate the row with the produced
 24317    glyphs.  */
 24318 static void
 24319 maybe_produce_line_number (struct it *it)
 24320 {
 24321   ptrdiff_t last_line = it->lnum;
 24322   ptrdiff_t start_from, bytepos;
 24323   ptrdiff_t this_line;
 24324   bool first_time = false;
 24325   ptrdiff_t beg_byte;
 24326   ptrdiff_t z_byte;
 24327   bool line_numbers_wide;
 24328   void *itdata = bidi_shelve_cache ();
 24329 
 24330   if (display_line_numbers_offset
 24331       && !display_line_numbers_widen
 24332       && !EQ (Vdisplay_line_numbers, Qvisual)
 24333       && !EQ (Vdisplay_line_numbers, Qrelative))
 24334     line_numbers_wide = true;
 24335   else
 24336     line_numbers_wide = display_line_numbers_widen;
 24337 
 24338   beg_byte = line_numbers_wide ? BEG_BYTE : BEGV_BYTE;
 24339   z_byte = line_numbers_wide ? Z_BYTE : ZV_BYTE;
 24340 
 24341   if (EQ (Vdisplay_line_numbers, Qvisual))
 24342     this_line = display_count_lines_visually (it);
 24343   else
 24344     {
 24345       if (!last_line)
 24346         {
 24347           /* If possible, reuse data cached by line-number-mode.  */
 24348           if (it->w->base_line_number > 0
 24349               && it->w->base_line_pos > 0
 24350               && it->w->base_line_pos <= IT_CHARPOS (*it)
 24351               /* line-number-mode always displays narrowed line
 24352                  numbers, so we cannot use its data if the user wants
 24353                  line numbers that disregard narrowing, or if the
 24354                  buffer's narrowing has just changed.  */
 24355               && !(line_numbers_wide
 24356                    && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
 24357               && !current_buffer->clip_changed)
 24358             {
 24359               start_from = CHAR_TO_BYTE (it->w->base_line_pos);
 24360               last_line = it->w->base_line_number - 1;
 24361             }
 24362           else
 24363             start_from = beg_byte;
 24364           if (!it->lnum_bytepos)
 24365             first_time = true;
 24366         }
 24367       else
 24368         start_from = it->lnum_bytepos;
 24369 
 24370       /* Paranoia: what if someone changes the narrowing since the
 24371          last time display_line was called?  Shouldn't really happen,
 24372          but who knows what some crazy Lisp invoked by :eval could do?  */
 24373       if (!(beg_byte <= start_from && start_from <= z_byte))
 24374         {
 24375           last_line = 0;
 24376           start_from = beg_byte;
 24377         }
 24378 
 24379       this_line =
 24380         last_line + display_count_lines_logically (start_from,
 24381                                                    IT_BYTEPOS (*it),
 24382                                                    IT_CHARPOS (*it), &bytepos);
 24383       eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
 24384       eassert (bytepos == IT_BYTEPOS (*it));
 24385     }
 24386 
 24387   /* Record the line number information.  */
 24388   if (this_line != last_line || !it->lnum_bytepos)
 24389     {
 24390       it->lnum = this_line;
 24391       it->lnum_bytepos = IT_BYTEPOS (*it);
 24392     }
 24393 
 24394   /* Produce the glyphs for the line number.  */
 24395   struct it tem_it;
 24396   char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
 24397   bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
 24398   ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
 24399   int lnum_face_id = merge_faces (it->w, Qline_number, 0, DEFAULT_FACE_ID);
 24400   int current_lnum_face_id
 24401     = merge_faces (it->w, Qline_number_current_line, 0, DEFAULT_FACE_ID);
 24402   /* From here onwards, we must prevent freeing realized faces, because
 24403      we are using the above 2 face IDs for the glyphs we produce.  */
 24404   bool save_free_realized_faces = inhibit_free_realized_faces;
 24405   inhibit_free_realized_faces = true;
 24406   /* Compute point's line number if needed.  */
 24407   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24408        || EQ (Vdisplay_line_numbers, Qvisual)
 24409        || lnum_face_id != current_lnum_face_id)
 24410       && !it->pt_lnum)
 24411     {
 24412       ptrdiff_t ignored;
 24413       if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
 24414         it->pt_lnum =
 24415           this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
 24416                                                      PT, &ignored);
 24417       else
 24418         it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
 24419                                                      &ignored);
 24420     }
 24421   /* Compute the required width if needed.  */
 24422   if (!it->lnum_width)
 24423     {
 24424       if (FIXNATP (Vdisplay_line_numbers_width))
 24425         it->lnum_width = XFIXNAT (Vdisplay_line_numbers_width);
 24426 
 24427       /* Max line number to be displayed cannot be more than the one
 24428          corresponding to the last row of the desired matrix.  */
 24429       ptrdiff_t max_lnum;
 24430 
 24431       if (NILP (Vdisplay_line_numbers_current_absolute)
 24432           && (EQ (Vdisplay_line_numbers, Qrelative)
 24433               || EQ (Vdisplay_line_numbers, Qvisual)))
 24434         /* We subtract one more because the current line is always
 24435            zero in this mode.  */
 24436         max_lnum = it->w->desired_matrix->nrows - 2;
 24437       else if (EQ (Vdisplay_line_numbers, Qvisual))
 24438         max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
 24439       else
 24440         max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
 24441       max_lnum = max (1, max_lnum);
 24442       it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
 24443       eassert (it->lnum_width > 0);
 24444     }
 24445   if (EQ (Vdisplay_line_numbers, Qrelative))
 24446     lnum_offset = it->pt_lnum;
 24447   else if (EQ (Vdisplay_line_numbers, Qvisual))
 24448     lnum_offset = 0;
 24449   else if (display_line_numbers_offset)
 24450     lnum_offset -= display_line_numbers_offset;
 24451 
 24452   /* Under 'relative', display the absolute line number for the
 24453      current line, unless the user requests otherwise.  */
 24454   ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
 24455   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24456        || EQ (Vdisplay_line_numbers, Qvisual))
 24457       && lnum_to_display == 0
 24458       && !NILP (Vdisplay_line_numbers_current_absolute))
 24459     lnum_to_display = it->pt_lnum + 1;
 24460   /* In L2R rows we need to append the blank separator, in R2L
 24461      rows we need to prepend it.  But this function is usually
 24462      called when no display elements were produced from the
 24463      following line, so the paragraph direction might be unknown.
 24464      Therefore we cheat and add 2 blanks, one on either side.  */
 24465   pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
 24466   strcat (lnum_buf, " ");
 24467 
 24468   /* Setup for producing the glyphs.  */
 24469   init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
 24470                  /* FIXME: Use specialized face.  */
 24471                  DEFAULT_FACE_ID);
 24472   scratch_glyph_row.reversed_p = false;
 24473   scratch_glyph_row.used[TEXT_AREA] = 0;
 24474   SET_TEXT_POS (tem_it.position, 0, 0);
 24475   tem_it.avoid_cursor_p = true;
 24476   tem_it.bidi_p = true;
 24477   tem_it.bidi_it.type = WEAK_EN;
 24478   /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
 24479      1 level in R2L paragraphs.  Emulate that, assuming we are in
 24480      an L2R paragraph.  */
 24481   tem_it.bidi_it.resolved_level = 2;
 24482 
 24483   /* We must leave space for 2 glyphs for continuation and truncation,
 24484      and at least one glyph for buffer text.  */
 24485   int width_limit =
 24486     tem_it.last_visible_x - tem_it.first_visible_x
 24487     - 3 * FRAME_COLUMN_WIDTH (it->f);
 24488 
 24489   tem_it.face_id = lnum_face_id;
 24490   /* Avoid displaying any face other than line-number on
 24491      empty lines beyond EOB.  */
 24492   if (lnum_face_id != current_lnum_face_id
 24493       && (EQ (Vdisplay_line_numbers, Qvisual)
 24494           ? this_line == 0
 24495           : this_line == it->pt_lnum)
 24496       && it->what != IT_EOB)
 24497     tem_it.face_id = current_lnum_face_id;
 24498   else if (!beyond_zv)
 24499     {
 24500       if (display_line_numbers_major_tick > 0
 24501           && (lnum_to_display % display_line_numbers_major_tick == 0))
 24502         tem_it.face_id = merge_faces (it->w, Qline_number_major_tick,
 24503                                       0, DEFAULT_FACE_ID);
 24504       else if (display_line_numbers_minor_tick > 0
 24505                && (lnum_to_display % display_line_numbers_minor_tick == 0))
 24506         tem_it.face_id = merge_faces (it->w, Qline_number_minor_tick,
 24507                                       0, DEFAULT_FACE_ID);
 24508     }
 24509 
 24510   /* Produce glyphs for the line number in a scratch glyph_row.  */
 24511   for (const char *p = lnum_buf; *p; p++)
 24512     {
 24513       /* For continuation lines and lines after ZV, instead of a line
 24514          number, produce a blank prefix of the same width.  */
 24515       if (beyond_zv
 24516           /* Don't display the same line number more than once.  */
 24517           || (!EQ (Vdisplay_line_numbers, Qvisual)
 24518               && (it->continuation_lines_width > 0
 24519                   || (this_line == last_line && !first_time))))
 24520         tem_it.c = tem_it.char_to_display = ' ';
 24521       else
 24522         tem_it.c = tem_it.char_to_display = *p;
 24523       tem_it.len = 1;
 24524       /* Make sure these glyphs will have a "position" of -1.  */
 24525       SET_TEXT_POS (tem_it.position, -1, -1);
 24526       PRODUCE_GLYPHS (&tem_it);
 24527 
 24528       /* Stop producing glyphs, and refrain from producing the line
 24529          number, if we don't have enough space on this line.  */
 24530       if (tem_it.current_x >= width_limit)
 24531         {
 24532           it->lnum_width = 0;
 24533           it->lnum_pixel_width = 0;
 24534           bidi_unshelve_cache (itdata, false);
 24535           inhibit_free_realized_faces = save_free_realized_faces;
 24536           return;
 24537         }
 24538     }
 24539 
 24540   inhibit_free_realized_faces = save_free_realized_faces;
 24541 
 24542   /* Record the width in pixels we need for the line number display.  */
 24543   it->lnum_pixel_width = tem_it.current_x;
 24544   /* Copy the produced glyphs into IT's glyph_row.  */
 24545   struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
 24546   struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
 24547   struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
 24548   short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
 24549 
 24550   eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
 24551 
 24552   for ( ; g < e; g++)
 24553     {
 24554       it->current_x += g->pixel_width;
 24555       /* The following is important when this function is called
 24556          from move_it_in_display_line_to: HPOS is incremented only
 24557          when we are in the visible portion of the glyph row.  */
 24558       if (it->current_x > it->first_visible_x)
 24559         it->hpos++;
 24560       if (p)
 24561         {
 24562           *p++ = *g;
 24563           (*u)++;
 24564         }
 24565     }
 24566 
 24567   /* Update IT's metrics due to glyphs produced for line numbers.
 24568      Don't do that for rows beyond ZV, to avoid displaying a cursor of
 24569      different dimensions there.  */
 24570   if (!beyond_zv)
 24571     {
 24572       if (it->glyph_row)
 24573         {
 24574           struct glyph_row *row = it->glyph_row;
 24575 
 24576           it->max_ascent = max (row->ascent, tem_it.max_ascent);
 24577           it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
 24578           it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
 24579           it->max_phys_descent = max (row->phys_height - row->phys_ascent,
 24580                                       tem_it.max_phys_descent);
 24581         }
 24582       else
 24583         {
 24584           it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
 24585           it->max_descent = max (it->max_descent, tem_it.max_descent);
 24586           it->max_phys_ascent = max (it->max_phys_ascent,
 24587                                      tem_it.max_phys_ascent);
 24588           it->max_phys_descent = max (it->max_phys_descent,
 24589                                       tem_it.max_phys_descent);
 24590         }
 24591     }
 24592 
 24593   it->line_number_produced_p = true;
 24594 
 24595   bidi_unshelve_cache (itdata, false);
 24596 }
 24597 
 24598 /* Return true if this glyph row needs a line number to be produced
 24599    for it.  */
 24600 static bool
 24601 should_produce_line_number (struct it *it)
 24602 {
 24603   if (NILP (Vdisplay_line_numbers))
 24604     return false;
 24605 
 24606   /* Don't display line numbers in minibuffer windows.  */
 24607   if (MINI_WINDOW_P (it->w))
 24608     return false;
 24609 
 24610 #ifdef HAVE_WINDOW_SYSTEM
 24611   /* Don't display line number in tooltip frames.  */
 24612   if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
 24613     return false;
 24614 #endif
 24615 
 24616   /* If the character at current position has a non-nil special
 24617      property, disable line numbers for this row.  This is for
 24618      packages such as company-mode, which need this for their tricky
 24619      layout, where line numbers get in the way.  */
 24620   Lisp_Object val = Fget_char_property (make_fixnum (IT_CHARPOS (*it)),
 24621                                         Qdisplay_line_numbers_disable,
 24622                                         it->window);
 24623   /* For ZV, we need to also look in empty overlays at that point,
 24624      because get-char-property always returns nil for ZV, except if
 24625      the property is in 'default-text-properties'.  */
 24626   if (NILP (val) && IT_CHARPOS (*it) >= ZV)
 24627     val = disable_line_numbers_overlay_at_eob ();
 24628   return NILP (val) ? true : false;
 24629 }
 24630 
 24631 /* Return true if ROW has no glyphs except those inserted by the
 24632    display engine.  This is needed for indicate-empty-lines and
 24633    similar features when the glyph row starts with glyphs which didn't
 24634    come from buffer or string.  */
 24635 static bool
 24636 row_text_area_empty (struct glyph_row *row)
 24637 {
 24638   if (!row->reversed_p)
 24639     {
 24640       for (struct glyph *g = row->glyphs[TEXT_AREA];
 24641            g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 24642            g++)
 24643         if (!NILP (g->object) || g->charpos > 0)
 24644           return false;
 24645     }
 24646   else
 24647     {
 24648       for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 24649            g > row->glyphs[TEXT_AREA];
 24650            g--)
 24651         if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
 24652           return false;
 24653     }
 24654 
 24655   return true;
 24656 }
 24657 
 24658 /* Construct the glyph row IT->glyph_row in the desired matrix of
 24659    IT->w from text at the current position of IT.  See dispextern.h
 24660    for an overview of struct it.  Value is true if
 24661    IT->glyph_row displays text, as opposed to a line displaying ZV
 24662    only.  CURSOR_VPOS is the window-relative vertical position of
 24663    the glyph row displaying the cursor, or -1 if unknown.  */
 24664 
 24665 static bool
 24666 display_line (struct it *it, int cursor_vpos)
 24667 {
 24668   struct glyph_row *row = it->glyph_row;
 24669   Lisp_Object overlay_arrow_string;
 24670   struct it wrap_it;
 24671   void *wrap_data = NULL;
 24672   bool may_wrap = false;
 24673   int wrap_x UNINIT;
 24674   int wrap_row_used = -1;
 24675   int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
 24676   int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
 24677   int wrap_row_extra_line_spacing UNINIT;
 24678   ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
 24679   ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
 24680   int cvpos;
 24681   ptrdiff_t min_pos = ZV + 1, max_pos = 0;
 24682   ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
 24683   bool pending_handle_line_prefix = false;
 24684   int tab_line = window_wants_tab_line (it->w);
 24685   int header_line = window_wants_header_line (it->w);
 24686   bool hscroll_this_line = (cursor_vpos >= 0
 24687                             && it->vpos == cursor_vpos - tab_line - header_line
 24688                             && hscrolling_current_line_p (it->w));
 24689   int first_visible_x = it->first_visible_x;
 24690   int last_visible_x = it->last_visible_x;
 24691   int x_incr = 0;
 24692 
 24693   /* We always start displaying at hpos zero even if hscrolled.  */
 24694   eassert (it->hpos == 0 && it->current_x == 0);
 24695 
 24696   if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
 24697       >= it->w->desired_matrix->nrows)
 24698     {
 24699       it->w->nrows_scale_factor++;
 24700       it->f->fonts_changed = true;
 24701       return false;
 24702     }
 24703 
 24704   /* Clear the result glyph row and enable it.  */
 24705   prepare_desired_row (it->w, row, false);
 24706 
 24707   row->y = it->current_y;
 24708   row->start = it->start;
 24709   row->continuation_lines_width = it->continuation_lines_width;
 24710   row->displays_text_p = true;
 24711   row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
 24712   it->starts_in_middle_of_char_p = false;
 24713   it->stretch_adjust = 0;
 24714   it->line_number_produced_p = false;
 24715 
 24716   /* If we are going to display the cursor's line, account for the
 24717      hscroll of that line.  We subtract the window's min_hscroll,
 24718      because that was already accounted for in init_iterator.  */
 24719   if (hscroll_this_line)
 24720     x_incr =
 24721       (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
 24722       * FRAME_COLUMN_WIDTH (it->f);
 24723 
 24724   bool line_number_needed = should_produce_line_number (it);
 24725 
 24726   /* Move over display elements that are not visible because we are
 24727      hscrolled.  This may stop at an x-position < first_visible_x
 24728      if the first glyph is partially visible or if we hit a line end.  */
 24729   if (it->current_x < it->first_visible_x + x_incr)
 24730     {
 24731       enum move_it_result move_result;
 24732 
 24733       this_line_min_pos = row->start.pos;
 24734       if (hscroll_this_line)
 24735         {
 24736           it->first_visible_x += x_incr;
 24737           it->last_visible_x  += x_incr;
 24738         }
 24739       if (current_buffer->long_line_optimizations_p
 24740           && it->line_wrap == TRUNCATE
 24741           && window_hscroll_limited (it->w, it->f) > large_hscroll_threshold)
 24742         {
 24743           /* Special optimization for very long and truncated lines
 24744              which are hscrolled far to the left: jump directly to the
 24745              (approximate) position that is visible, instead of slowly
 24746              walking there.  */
 24747           ptrdiff_t chars_to_skip =
 24748             it->first_visible_x / FRAME_COLUMN_WIDTH (it->f);
 24749           move_result = fast_move_it_horizontally (it, chars_to_skip);
 24750 
 24751           if (move_result == MOVE_X_REACHED)
 24752             it->current_x = it->first_visible_x;
 24753           else  /* use arbitrary value < first_visible_x */
 24754             it->current_x = it->first_visible_x - FRAME_COLUMN_WIDTH (it->f);
 24755         }
 24756       else
 24757         move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
 24758                                                   MOVE_TO_POS | MOVE_TO_X);
 24759       /* If we are under a large hscroll, move_it_in_display_line_to
 24760          could hit the end of the line without reaching
 24761          first_visible_x.  Pretend that we did reach it.  This is
 24762          especially important on a TTY, where we will call
 24763          extend_face_to_end_of_line, which needs to know how many
 24764          blank glyphs to produce.  */
 24765       if (it->current_x < it->first_visible_x
 24766           && (move_result == MOVE_NEWLINE_OR_CR
 24767               || move_result == MOVE_POS_MATCH_OR_ZV))
 24768         it->current_x = it->first_visible_x;
 24769 
 24770       /* In case move_it_in_display_line_to above "produced" the line
 24771          number.  */
 24772       it->line_number_produced_p = false;
 24773 
 24774       /* Record the smallest positions seen while we moved over
 24775          display elements that are not visible.  This is needed by
 24776          redisplay_internal for optimizing the case where the cursor
 24777          stays inside the same line.  The rest of this function only
 24778          considers positions that are actually displayed, so
 24779          RECORD_MAX_MIN_POS will not otherwise record positions that
 24780          are hscrolled to the left of the left edge of the window.  */
 24781       min_pos = CHARPOS (this_line_min_pos);
 24782       min_bpos = BYTEPOS (this_line_min_pos);
 24783 
 24784       /* Produce line number, if needed.  */
 24785       if (line_number_needed)
 24786         maybe_produce_line_number (it);
 24787     }
 24788   else if (it->area == TEXT_AREA)
 24789     {
 24790       /* Line numbers should precede the line-prefix or wrap-prefix.  */
 24791       if (line_number_needed)
 24792         maybe_produce_line_number (it);
 24793 
 24794       /* We only do this when not calling move_it_in_display_line_to
 24795          above, because that function calls itself handle_line_prefix.  */
 24796       handle_line_prefix (it);
 24797     }
 24798   else
 24799     {
 24800       /* Line-prefix and wrap-prefix are always displayed in the text
 24801          area.  But if this is the first call to display_line after
 24802          init_iterator, the iterator might have been set up to write
 24803          into a marginal area, e.g. if the line begins with some
 24804          display property that writes to the margins.  So we need to
 24805          wait with the call to handle_line_prefix until whatever
 24806          writes to the margin has done its job.  */
 24807       pending_handle_line_prefix = true;
 24808     }
 24809 
 24810   /* Get the initial row height.  This is either the height of the
 24811      text hscrolled, if there is any, or zero.  */
 24812   row->ascent = it->max_ascent;
 24813   row->height = it->max_ascent + it->max_descent;
 24814   row->phys_ascent = it->max_phys_ascent;
 24815   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 24816   row->extra_line_spacing = it->max_extra_line_spacing;
 24817 
 24818 /* Utility macro to record max and min buffer positions seen until now.  */
 24819 #define RECORD_MAX_MIN_POS(IT)                                  \
 24820   do                                                            \
 24821     {                                                           \
 24822       bool composition_p                                        \
 24823         = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
 24824       ptrdiff_t current_pos =                                   \
 24825         composition_p ? (IT)->cmp_it.charpos                    \
 24826                       : IT_CHARPOS (*(IT));                     \
 24827       ptrdiff_t current_bpos =                                  \
 24828         composition_p ? CHAR_TO_BYTE (current_pos)              \
 24829                       : IT_BYTEPOS (*(IT));                     \
 24830       if (current_pos < min_pos)                                \
 24831         {                                                       \
 24832           min_pos = current_pos;                                \
 24833           min_bpos = current_bpos;                              \
 24834         }                                                       \
 24835       if (IT_CHARPOS (*it) > max_pos)                           \
 24836         {                                                       \
 24837           max_pos = IT_CHARPOS (*it);                           \
 24838           max_bpos = IT_BYTEPOS (*it);                          \
 24839         }                                                       \
 24840     }                                                           \
 24841   while (false)
 24842 
 24843   /* Loop generating characters.  The loop is left with IT on the next
 24844      character to display.  */
 24845   while (true)
 24846     {
 24847       int n_glyphs_before, hpos_before, x_before;
 24848       int x, nglyphs;
 24849       int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
 24850 
 24851       /* Retrieve the next thing to display.  Value is false if end of
 24852          buffer reached.  */
 24853       if (!get_next_display_element (it))
 24854         {
 24855           bool row_has_glyphs = false;
 24856           /* Maybe add a space at the end of this line that is used to
 24857              display the cursor there under X.  Set the charpos of the
 24858              first glyph of blank lines not corresponding to any text
 24859              to -1.  */
 24860           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 24861             row->exact_window_width_line_p = true;
 24862           else if ((append_space_for_newline (it, true)
 24863                     && row->used[TEXT_AREA] == 1)
 24864                    || row->used[TEXT_AREA] == 0
 24865                    || (row_has_glyphs = row_text_area_empty (row)))
 24866             {
 24867               row->glyphs[TEXT_AREA]->charpos = -1;
 24868               /* Don't reset the displays_text_p flag if we are
 24869                  displaying line numbers or line-prefix.  */
 24870               if (!row_has_glyphs)
 24871                 row->displays_text_p = false;
 24872 
 24873               if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
 24874                   && (!MINI_WINDOW_P (it->w)))
 24875                 row->indicate_empty_line_p = true;
 24876             }
 24877 
 24878           it->continuation_lines_width = 0;
 24879           /* Reset those iterator values set from display property
 24880              values.  This is for the case when the display property
 24881              ends at ZV, and is not a replacing property, so pop_it is
 24882              not called.  */
 24883           it->font_height = Qnil;
 24884           it->voffset = 0;
 24885           row->ends_at_zv_p = true;
 24886           /* A row that displays right-to-left text must always have
 24887              its last face extended all the way to the end of line,
 24888              even if this row ends in ZV, because we still write to
 24889              the screen left to right.  We also need to extend the
 24890              last face if the default face is remapped to some
 24891              different face, otherwise the functions that clear
 24892              portions of the screen will clear with the default face's
 24893              background color.  */
 24894           if (row->reversed_p
 24895               || lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
 24896               != DEFAULT_FACE_ID)
 24897             extend_face_to_end_of_line (it);
 24898           break;
 24899         }
 24900 
 24901       /* Now, get the metrics of what we want to display.  This also
 24902          generates glyphs in `row' (which is IT->glyph_row).  */
 24903       n_glyphs_before = row->used[TEXT_AREA];
 24904       x = it->current_x;
 24905 
 24906       /* Remember the line height so far in case the next element doesn't
 24907          fit on the line.  */
 24908       if (it->line_wrap != TRUNCATE)
 24909         {
 24910           ascent = it->max_ascent;
 24911           descent = it->max_descent;
 24912           phys_ascent = it->max_phys_ascent;
 24913           phys_descent = it->max_phys_descent;
 24914 
 24915           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
 24916             {
 24917               bool next_may_wrap = may_wrap;
 24918               /* Can we wrap after this character?  */
 24919               if (char_can_wrap_after (it))
 24920                 next_may_wrap = true;
 24921               else
 24922                 next_may_wrap = false;
 24923               /* Can we wrap here? */
 24924               if (may_wrap && char_can_wrap_before (it))
 24925                 {
 24926                   SAVE_IT (wrap_it, *it, wrap_data);
 24927                   wrap_x = x;
 24928                   wrap_row_used = row->used[TEXT_AREA];
 24929                   wrap_row_ascent = row->ascent;
 24930                   wrap_row_height = row->height;
 24931                   wrap_row_phys_ascent = row->phys_ascent;
 24932                   wrap_row_phys_height = row->phys_height;
 24933                   wrap_row_extra_line_spacing = row->extra_line_spacing;
 24934                   wrap_row_min_pos = min_pos;
 24935                   wrap_row_min_bpos = min_bpos;
 24936                   wrap_row_max_pos = max_pos;
 24937                   wrap_row_max_bpos = max_bpos;
 24938                 }
 24939               /* Update may_wrap for the next iteration.  */
 24940               may_wrap = next_may_wrap;
 24941             }
 24942         }
 24943 
 24944       PRODUCE_GLYPHS (it);
 24945 
 24946       /* If this display element was in marginal areas, continue with
 24947          the next one.  */
 24948       if (it->area != TEXT_AREA)
 24949         {
 24950           row->ascent = max (row->ascent, it->max_ascent);
 24951           row->height = max (row->height, it->max_ascent + it->max_descent);
 24952           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 24953           row->phys_height = max (row->phys_height,
 24954                                   it->max_phys_ascent + it->max_phys_descent);
 24955           row->extra_line_spacing = max (row->extra_line_spacing,
 24956                                          it->max_extra_line_spacing);
 24957           set_iterator_to_next (it, true);
 24958           /* If we didn't handle the line/wrap prefix above, and the
 24959              call to set_iterator_to_next just switched to TEXT_AREA,
 24960              process the prefix now.  */
 24961           if (it->area == TEXT_AREA && pending_handle_line_prefix)
 24962             {
 24963               /* Line numbers should precede the line-prefix or wrap-prefix.  */
 24964               if (line_number_needed)
 24965                 maybe_produce_line_number (it);
 24966 
 24967               pending_handle_line_prefix = false;
 24968               handle_line_prefix (it);
 24969             }
 24970           continue;
 24971         }
 24972 
 24973       /* Does the display element fit on the line?  If we truncate
 24974          lines, we should draw past the right edge of the window.  If
 24975          we don't truncate, we want to stop so that we can display the
 24976          continuation glyph before the right margin.  If lines are
 24977          continued, there are two possible strategies for characters
 24978          resulting in more than 1 glyph (e.g. tabs): Display as many
 24979          glyphs as possible in this line and leave the rest for the
 24980          continuation line, or display the whole element in the next
 24981          line.  Original redisplay did the former, so we do it also.  */
 24982       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 24983       hpos_before = it->hpos;
 24984       x_before = x;
 24985 
 24986       if (/* Not a newline.  */
 24987           nglyphs > 0
 24988           /* Glyphs produced fit entirely in the line.  */
 24989           && it->current_x < it->last_visible_x)
 24990         {
 24991           it->hpos += nglyphs;
 24992           row->ascent = max (row->ascent, it->max_ascent);
 24993           row->height = max (row->height, it->max_ascent + it->max_descent);
 24994           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 24995           row->phys_height = max (row->phys_height,
 24996                                   it->max_phys_ascent + it->max_phys_descent);
 24997           row->extra_line_spacing = max (row->extra_line_spacing,
 24998                                          it->max_extra_line_spacing);
 24999           if (it->current_x - it->pixel_width < it->first_visible_x
 25000               /* When line numbers are displayed, row->x should not be
 25001                  offset, as the first glyph after the line number can
 25002                  never be partially visible.  */
 25003               && !line_number_needed
 25004               /* In R2L rows, we arrange in extend_face_to_end_of_line
 25005                  to add a right offset to the line, by a suitable
 25006                  change to the stretch glyph that is the leftmost
 25007                  glyph of the line.  */
 25008               && !row->reversed_p)
 25009             row->x = x - it->first_visible_x;
 25010           /* Record the maximum and minimum buffer positions seen so
 25011              far in glyphs that will be displayed by this row.  */
 25012           if (it->bidi_p)
 25013             RECORD_MAX_MIN_POS (it);
 25014         }
 25015       else
 25016         {
 25017           int i, new_x;
 25018           struct glyph *glyph;
 25019 
 25020           for (i = 0; i < nglyphs; ++i, x = new_x)
 25021             {
 25022               /* Identify the glyphs added by the last call to
 25023                  PRODUCE_GLYPHS.  In R2L rows, they are prepended to
 25024                  the previous glyphs.  */
 25025               if (!row->reversed_p)
 25026                 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 25027               else
 25028                 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
 25029               new_x = x + glyph->pixel_width;
 25030 
 25031               if (/* Lines are continued.  */
 25032                   it->line_wrap != TRUNCATE
 25033                   && (/* Glyph doesn't fit on the line.  */
 25034                       new_x > it->last_visible_x
 25035                       /* Or it fits exactly on a window system frame.  */
 25036                       || (new_x == it->last_visible_x
 25037                           && FRAME_WINDOW_P (it->f)
 25038                           && (row->reversed_p
 25039                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25040                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 25041                 {
 25042                   /* End of a continued line.  */
 25043 
 25044                   if (it->hpos == 0
 25045                       || (new_x == it->last_visible_x
 25046                           && FRAME_WINDOW_P (it->f)
 25047                           && (row->reversed_p
 25048                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25049                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
 25050                     {
 25051                       /* Current glyph is the only one on the line or
 25052                          fits exactly on the line.  We must continue
 25053                          the line because we can't draw the cursor
 25054                          after the glyph.  */
 25055                       row->continued_p = true;
 25056                       it->current_x = new_x;
 25057                       it->continuation_lines_width += new_x;
 25058                       ++it->hpos;
 25059                       if (i == nglyphs - 1)
 25060                         {
 25061                           /* If line-wrap is on, check if a previous
 25062                              wrap point was found.  */
 25063                           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 25064                               && wrap_row_used > 0 /* Found.  */
 25065                               /* Even if there is a previous wrap
 25066                                  point, continue the line here as
 25067                                  usual, if (i) the previous character
 25068                                  allows wrapping after it, AND (ii)
 25069                                  the current character allows wrapping
 25070                                  before it.  Because this is a valid
 25071                                  break point, we can just continue to
 25072                                  the next line at here, there is no
 25073                                  need to wrap early at the previous
 25074                                  wrap point.  */
 25075                               && (!may_wrap || !char_can_wrap_before (it)))
 25076                             goto back_to_wrap;
 25077 
 25078                           /* Record the maximum and minimum buffer
 25079                              positions seen so far in glyphs that will be
 25080                              displayed by this row.  */
 25081                           if (it->bidi_p)
 25082                             RECORD_MAX_MIN_POS (it);
 25083                           set_iterator_to_next (it, true);
 25084                           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25085                             {
 25086                               if (!get_next_display_element (it))
 25087                                 {
 25088                                   row->exact_window_width_line_p = true;
 25089                                   it->continuation_lines_width = 0;
 25090                                   it->font_height = Qnil;
 25091                                   it->voffset = 0;
 25092                                   row->continued_p = false;
 25093                                   row->ends_at_zv_p = true;
 25094                                 }
 25095                               else if (ITERATOR_AT_END_OF_LINE_P (it))
 25096                                 {
 25097                                   row->continued_p = false;
 25098                                   row->exact_window_width_line_p = true;
 25099                                 }
 25100                               /* If line-wrap is on, check if a
 25101                                  previous wrap point was found.  */
 25102                               else if (wrap_row_used > 0
 25103                                        /* Even if there is a previous
 25104                                           wrap point, continue the
 25105                                           line here as usual, if (i)
 25106                                           the previous character was a
 25107                                           space or tab AND (ii) the
 25108                                           current character is not,
 25109                                           AND (iii) the current
 25110                                           character allows wrapping
 25111                                           before it.  */
 25112                                        && (!may_wrap || !char_can_wrap_before (it)))
 25113                                 goto back_to_wrap;
 25114 
 25115                             }
 25116                         }
 25117                       else if (it->bidi_p)
 25118                         RECORD_MAX_MIN_POS (it);
 25119                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25120                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25121                         extend_face_to_end_of_line (it);
 25122                     }
 25123                   else if (CHAR_GLYPH_PADDING_P (*glyph)
 25124                            && !FRAME_WINDOW_P (it->f))
 25125                     {
 25126                       /* A padding glyph that doesn't fit on this line.
 25127                          This means the whole character doesn't fit
 25128                          on the line.  */
 25129                       if (row->reversed_p)
 25130                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25131                                                - n_glyphs_before);
 25132                       row->used[TEXT_AREA] = n_glyphs_before;
 25133 
 25134                       /* Fill the rest of the row with continuation
 25135                          glyphs like in 20.x.  */
 25136                       while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
 25137                              < row->glyphs[1 + TEXT_AREA])
 25138                         produce_special_glyphs (it, IT_CONTINUATION);
 25139 
 25140                       row->continued_p = true;
 25141                       it->current_x = x_before;
 25142                       it->continuation_lines_width += x_before;
 25143 
 25144                       /* Restore the height to what it was before the
 25145                          element not fitting on the line.  */
 25146                       it->max_ascent = ascent;
 25147                       it->max_descent = descent;
 25148                       it->max_phys_ascent = phys_ascent;
 25149                       it->max_phys_descent = phys_descent;
 25150                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25151                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25152                         extend_face_to_end_of_line (it);
 25153                     }
 25154                   else if (wrap_row_used > 0)
 25155                     {
 25156                     back_to_wrap:
 25157                       if (row->reversed_p)
 25158                         unproduce_glyphs (it,
 25159                                           row->used[TEXT_AREA] - wrap_row_used);
 25160                       RESTORE_IT (it, &wrap_it, wrap_data);
 25161                       it->continuation_lines_width += wrap_x;
 25162                       row->used[TEXT_AREA] = wrap_row_used;
 25163                       row->ascent = wrap_row_ascent;
 25164                       row->height = wrap_row_height;
 25165                       row->phys_ascent = wrap_row_phys_ascent;
 25166                       row->phys_height = wrap_row_phys_height;
 25167                       row->extra_line_spacing = wrap_row_extra_line_spacing;
 25168                       min_pos = wrap_row_min_pos;
 25169                       min_bpos = wrap_row_min_bpos;
 25170                       max_pos = wrap_row_max_pos;
 25171                       max_bpos = wrap_row_max_bpos;
 25172                       row->continued_p = true;
 25173                       row->ends_at_zv_p = false;
 25174                       row->exact_window_width_line_p = false;
 25175 
 25176                       /* Make sure that a non-default face is extended
 25177                          up to the right margin of the window.  */
 25178                       extend_face_to_end_of_line (it);
 25179                     }
 25180                   else if ((it->what == IT_CHARACTER
 25181                             || it->what == IT_STRETCH
 25182                             || it->what == IT_COMPOSITION)
 25183                            && it->c == '\t' && FRAME_WINDOW_P (it->f))
 25184                     {
 25185                       /* A TAB that extends past the right edge of the
 25186                          window.  This produces a single glyph on
 25187                          window system frames.  We leave the glyph in
 25188                          this row and let it fill the row, but don't
 25189                          consume the TAB.  */
 25190                       if ((row->reversed_p
 25191                            ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25192                            : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25193                         produce_special_glyphs (it, IT_CONTINUATION);
 25194                       it->continuation_lines_width += it->last_visible_x;
 25195                       row->ends_in_middle_of_char_p = true;
 25196                       row->continued_p = true;
 25197                       glyph->pixel_width = it->last_visible_x - x;
 25198                       it->starts_in_middle_of_char_p = true;
 25199                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25200                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25201                         extend_face_to_end_of_line (it);
 25202                     }
 25203                   else
 25204                     {
 25205                       /* Something other than a TAB that draws past
 25206                          the right edge of the window.  Restore
 25207                          positions to values before the element.  */
 25208                       if (row->reversed_p)
 25209                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25210                                                - (n_glyphs_before + i));
 25211                       row->used[TEXT_AREA] = n_glyphs_before + i;
 25212 
 25213                       /* Display continuation glyphs.  */
 25214                       it->current_x = x_before;
 25215                       it->continuation_lines_width += x;
 25216                       if (!FRAME_WINDOW_P (it->f)
 25217                           || (row->reversed_p
 25218                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25219                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25220                         produce_special_glyphs (it, IT_CONTINUATION);
 25221                       row->continued_p = true;
 25222 
 25223                       extend_face_to_end_of_line (it);
 25224 
 25225                       if (nglyphs > 1 && i > 0)
 25226                         {
 25227                           row->ends_in_middle_of_char_p = true;
 25228                           it->starts_in_middle_of_char_p = true;
 25229                         }
 25230 
 25231                       /* Restore the height to what it was before the
 25232                          element not fitting on the line.  */
 25233                       it->max_ascent = ascent;
 25234                       it->max_descent = descent;
 25235                       it->max_phys_ascent = phys_ascent;
 25236                       it->max_phys_descent = phys_descent;
 25237                     }
 25238 
 25239                   break;
 25240                 }
 25241               else if (new_x > it->first_visible_x)
 25242                 {
 25243                   /* Increment number of glyphs actually displayed.  */
 25244                   ++it->hpos;
 25245 
 25246                   /* Record the maximum and minimum buffer positions
 25247                      seen so far in glyphs that will be displayed by
 25248                      this row.  */
 25249                   if (it->bidi_p)
 25250                     RECORD_MAX_MIN_POS (it);
 25251 
 25252                   if (x < it->first_visible_x && !row->reversed_p
 25253                       && !line_number_needed)
 25254                     /* Glyph is partially visible, i.e. row starts at
 25255                        negative X position.  Don't do that in R2L
 25256                        rows, where we arrange to add a right offset to
 25257                        the line in extend_face_to_end_of_line, by a
 25258                        suitable change to the stretch glyph that is
 25259                        the leftmost glyph of the line.  */
 25260                     row->x = x - it->first_visible_x;
 25261                   /* When the last glyph of an R2L row only fits
 25262                      partially on the line, we need to set row->x to a
 25263                      negative offset, so that the leftmost glyph is
 25264                      the one that is partially visible.  But if we are
 25265                      going to produce the truncation glyph, this will
 25266                      be taken care of in produce_special_glyphs.  */
 25267                   if (row->reversed_p
 25268                       && new_x > it->last_visible_x
 25269                       && !line_number_needed
 25270                       && !(it->line_wrap == TRUNCATE
 25271                            && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
 25272                     {
 25273                       eassert (FRAME_WINDOW_P (it->f));
 25274                       row->x = it->last_visible_x - new_x;
 25275                     }
 25276                 }
 25277               else
 25278                 {
 25279                   /* Glyph is completely off the left margin of the
 25280                      window.  This should not happen because of the
 25281                      move_it_in_display_line at the start of this
 25282                      function, unless the text display area of the
 25283                      window is empty.  */
 25284                   eassert (it->first_visible_x <= it->last_visible_x);
 25285                 }
 25286             }
 25287           /* Even if this display element produced no glyphs at all,
 25288              we want to record its position.  */
 25289           if (it->bidi_p && nglyphs == 0)
 25290             RECORD_MAX_MIN_POS (it);
 25291 
 25292           row->ascent = max (row->ascent, it->max_ascent);
 25293           row->height = max (row->height, it->max_ascent + it->max_descent);
 25294           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25295           row->phys_height = max (row->phys_height,
 25296                                   it->max_phys_ascent + it->max_phys_descent);
 25297           row->extra_line_spacing = max (row->extra_line_spacing,
 25298                                          it->max_extra_line_spacing);
 25299 
 25300           /* End of this display line if row is continued.  */
 25301           if (row->continued_p || row->ends_at_zv_p)
 25302             break;
 25303         }
 25304 
 25305     at_end_of_line:
 25306       /* Is this a line end?  If yes, we're also done, after making
 25307          sure that a non-default face is extended up to the right
 25308          margin of the window.  */
 25309       if (ITERATOR_AT_END_OF_LINE_P (it))
 25310         {
 25311           int used_before = row->used[TEXT_AREA];
 25312 
 25313           row->ends_in_newline_from_string_p = STRINGP (it->object);
 25314 
 25315           /* Add a space at the end of the line that is used to
 25316              display the cursor there.  */
 25317           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25318             append_space_for_newline (it, false);
 25319 
 25320           /* Extend the face to the end of the line.  */
 25321           extend_face_to_end_of_line (it);
 25322 
 25323           /* Make sure we have the position.  */
 25324           if (used_before == 0)
 25325             row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
 25326 
 25327           /* Record the position of the newline, for use in
 25328              find_row_edges.  */
 25329           it->eol_pos = it->current.pos;
 25330 
 25331           /* Consume the line end.  This skips over invisible lines.  */
 25332           set_iterator_to_next (it, true);
 25333           it->continuation_lines_width = 0;
 25334           break;
 25335         }
 25336 
 25337       /* Detect overly-wide wrap-prefixes made of (space ...) display
 25338          properties.  When such a wrap prefix reaches past the right
 25339          margin of the window, we need to avoid the call to
 25340          set_iterator_to_next below, so that it->line_wrap is left at
 25341          its TRUNCATE value wisely set by handle_line_prefix.
 25342          Otherwise, set_iterator_to_next will pop the iterator stack,
 25343          restore it->line_wrap, and redisplay might infloop.  */
 25344       bool overwide_wrap_prefix =
 25345         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 25346         && it->sp > 0 && it->method == GET_FROM_STRETCH
 25347         && it->current_x >= it->last_visible_x
 25348         && it->continuation_lines_width > 0
 25349         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 25350 
 25351       /* Proceed with next display element.  Note that this skips
 25352          over lines invisible because of selective display.  */
 25353       if (!overwide_wrap_prefix)
 25354         set_iterator_to_next (it, true);
 25355 
 25356       /* If we truncate lines, we are done when the last displayed
 25357          glyphs reach past the right margin of the window.  */
 25358       if (it->line_wrap == TRUNCATE
 25359           && ((FRAME_WINDOW_P (it->f)
 25360                /* Images are preprocessed in produce_image_glyph such
 25361                   that they are cropped at the right edge of the
 25362                   window, so an image glyph will always end exactly at
 25363                   last_visible_x, even if there's no right fringe.  */
 25364                && ((row->reversed_p
 25365                     ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25366                     : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
 25367                    || it->what == IT_IMAGE))
 25368               ? (it->current_x >= it->last_visible_x)
 25369               : (it->current_x > it->last_visible_x)))
 25370         {
 25371           /* Maybe add truncation glyphs.  */
 25372           if (!FRAME_WINDOW_P (it->f)
 25373               || (row->reversed_p
 25374                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25375                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25376             {
 25377               int i, n;
 25378 
 25379               if (!row->reversed_p)
 25380                 {
 25381                   for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
 25382                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25383                       break;
 25384                 }
 25385               else
 25386                 {
 25387                   for (i = 0; i < row->used[TEXT_AREA]; i++)
 25388                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25389                       break;
 25390                   /* Remove any padding glyphs at the front of ROW, to
 25391                      make room for the truncation glyphs we will be
 25392                      adding below.  The loop below always inserts at
 25393                      least one truncation glyph, so also remove the
 25394                      last glyph added to ROW.  */
 25395                   unproduce_glyphs (it, i + 1);
 25396                   /* Adjust i for the loop below.  */
 25397                   i = row->used[TEXT_AREA] - (i + 1);
 25398                 }
 25399 
 25400               /* produce_special_glyphs overwrites the last glyph, so
 25401                  we don't want that if we want to keep that last
 25402                  glyph, which means it's an image.  */
 25403               if (it->current_x > it->last_visible_x)
 25404                 {
 25405                   it->current_x = x_before;
 25406                   if (!FRAME_WINDOW_P (it->f))
 25407                     {
 25408                       for (n = row->used[TEXT_AREA]; i < n; ++i)
 25409                         {
 25410                           row->used[TEXT_AREA] = i;
 25411                           produce_special_glyphs (it, IT_TRUNCATION);
 25412                         }
 25413                     }
 25414                   else
 25415                     {
 25416                       row->used[TEXT_AREA] = i;
 25417                       produce_special_glyphs (it, IT_TRUNCATION);
 25418                     }
 25419                   it->hpos = hpos_before;
 25420                 }
 25421             }
 25422           else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25423             {
 25424               /* Don't truncate if we can overflow newline into fringe.  */
 25425               if (!get_next_display_element (it))
 25426                 {
 25427                   it->continuation_lines_width = 0;
 25428                   it->font_height = Qnil;
 25429                   it->voffset = 0;
 25430                   row->ends_at_zv_p = true;
 25431                   row->exact_window_width_line_p = true;
 25432                   break;
 25433                 }
 25434               if (ITERATOR_AT_END_OF_LINE_P (it))
 25435                 {
 25436                   row->exact_window_width_line_p = true;
 25437                   goto at_end_of_line;
 25438                 }
 25439               it->current_x = x_before;
 25440               it->hpos = hpos_before;
 25441             }
 25442 
 25443           row->truncated_on_right_p = true;
 25444           it->continuation_lines_width = 0;
 25445           reseat_at_next_visible_line_start (it, false);
 25446           /* We insist below that IT's position be at ZV because in
 25447              bidi-reordered lines the character at visible line start
 25448              might not be the character that follows the newline in
 25449              the logical order.  */
 25450           if (IT_BYTEPOS (*it) > BEG_BYTE)
 25451             row->ends_at_zv_p =
 25452               IT_BYTEPOS (*it) >= ZV_BYTE
 25453               && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n');
 25454           else
 25455             row->ends_at_zv_p = false;
 25456           break;
 25457         }
 25458     }
 25459 
 25460   if (wrap_data)
 25461     bidi_unshelve_cache (wrap_data, true);
 25462 
 25463   /* If line is not empty and hscrolled, maybe insert truncation glyphs
 25464      at the left window margin.  */
 25465   if (it->first_visible_x
 25466       && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
 25467     {
 25468       if (!FRAME_WINDOW_P (it->f)
 25469           || (((row->reversed_p
 25470                 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 25471                 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 25472               /* Don't let insert_left_trunc_glyphs overwrite the
 25473                  first glyph of the row if it is an image.  */
 25474               && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
 25475         insert_left_trunc_glyphs (it);
 25476       row->truncated_on_left_p = true;
 25477     }
 25478 
 25479   /* Remember the position at which this line ends.
 25480 
 25481      BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
 25482      cannot be before the call to find_row_edges below, since that is
 25483      where these positions are determined. */
 25484   row->end = it->current;
 25485   if (!it->bidi_p)
 25486     {
 25487       row->minpos = row->start.pos;
 25488       row->maxpos = row->end.pos;
 25489     }
 25490   else
 25491     {
 25492       /* ROW->minpos and ROW->maxpos must be the smallest and
 25493          `1 + the largest' buffer positions in ROW.  But if ROW was
 25494          bidi-reordered, these two positions can be anywhere in the
 25495          row, so we must determine them now.  */
 25496       find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
 25497     }
 25498 
 25499   /* If the start of this line is the overlay arrow-position, then
 25500      mark this glyph row as the one containing the overlay arrow.
 25501      This is clearly a mess with variable size fonts.  It would be
 25502      better to let it be displayed like cursors under X.  */
 25503   if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
 25504       && (overlay_arrow_string = overlay_arrow_at_row (it, row),
 25505           !NILP (overlay_arrow_string)))
 25506     {
 25507       /* Overlay arrow in window redisplay is a fringe bitmap.  */
 25508       if (STRINGP (overlay_arrow_string))
 25509         {
 25510           struct glyph_row *arrow_row
 25511             = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
 25512           struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
 25513           struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
 25514           struct glyph *p = row->glyphs[TEXT_AREA];
 25515           struct glyph *p2, *end;
 25516 
 25517           /* Copy the arrow glyphs.  */
 25518           while (glyph < arrow_end)
 25519             *p++ = *glyph++;
 25520 
 25521           /* Throw away padding glyphs.  */
 25522           p2 = p;
 25523           end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 25524           while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
 25525             ++p2;
 25526           if (p2 > p)
 25527             {
 25528               while (p2 < end)
 25529                 *p++ = *p2++;
 25530               row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
 25531             }
 25532         }
 25533       else
 25534         {
 25535           eassert (FIXNUMP (overlay_arrow_string));
 25536           row->overlay_arrow_bitmap = XFIXNUM (overlay_arrow_string);
 25537         }
 25538       overlay_arrow_seen = true;
 25539     }
 25540 
 25541   /* Highlight trailing whitespace.  */
 25542   if (!NILP (Vshow_trailing_whitespace))
 25543     highlight_trailing_whitespace (it);
 25544 
 25545   /* Compute pixel dimensions of this line.  */
 25546   compute_line_metrics (it);
 25547 
 25548   /* Implementation note: No changes in the glyphs of ROW or in their
 25549      faces can be done past this point, because compute_line_metrics
 25550      computes ROW's hash value and stores it within the glyph_row
 25551      structure.  */
 25552 
 25553   /* Record whether this row ends inside an ellipsis.  */
 25554   row->ends_in_ellipsis_p
 25555     = (it->method == GET_FROM_DISPLAY_VECTOR
 25556        && it->ellipsis_p);
 25557 
 25558   /* Save fringe bitmaps in this row.  */
 25559   row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
 25560   row->left_user_fringe_face_id = it->left_user_fringe_face_id;
 25561   row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
 25562   row->right_user_fringe_face_id = it->right_user_fringe_face_id;
 25563 
 25564   it->left_user_fringe_bitmap = 0;
 25565   it->left_user_fringe_face_id = 0;
 25566   it->right_user_fringe_bitmap = 0;
 25567   it->right_user_fringe_face_id = 0;
 25568 
 25569   /* When they turn off tooltip-mode on a GUI frame, we call 'message'
 25570      with message-truncate-lines bound to non-nil, which produces
 25571      truncation bitmaps on the fringe.  Force redrawing of the fringes
 25572      in that case, to make sure the fringe bitmaps are removed when a
 25573      shorter message is displayed.  */
 25574   if (MINI_WINDOW_P (it->w) && it->line_wrap == TRUNCATE
 25575       && FRAME_WINDOW_P (it->f) && !cursor_in_echo_area)
 25576     row->redraw_fringe_bitmaps_p = true;
 25577 
 25578   /* Maybe set the cursor.  */
 25579   cvpos = it->w->cursor.vpos;
 25580   if ((cvpos < 0
 25581        /* In bidi-reordered rows, keep checking for proper cursor
 25582           position even if one has been found already, because buffer
 25583           positions in such rows change non-linearly with ROW->VPOS,
 25584           when a line is continued.  One exception: when we are at ZV,
 25585           display cursor on the first suitable glyph row, since all
 25586           the empty rows after that also have their position set to ZV.  */
 25587        /* FIXME: Revisit this when glyph ``spilling'' in continuation
 25588           lines' rows is implemented for bidi-reordered rows.  */
 25589        || (it->bidi_p
 25590            && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
 25591       && PT >= MATRIX_ROW_START_CHARPOS (row)
 25592       && PT <= MATRIX_ROW_END_CHARPOS (row)
 25593       && cursor_row_p (row))
 25594     set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
 25595 
 25596   /* Prepare for the next line.  This line starts horizontally at (X
 25597      HPOS) = (0 0).  Vertical positions are incremented.  As a
 25598      convenience for the caller, IT->glyph_row is set to the next
 25599      row to be used.  */
 25600   it->current_x = it->hpos = 0;
 25601   it->current_y += row->height;
 25602   /* Restore the first and last visible X if we adjusted them for
 25603      current-line hscrolling.  */
 25604   if (hscroll_this_line)
 25605     {
 25606       it->first_visible_x = first_visible_x;
 25607       it->last_visible_x  = last_visible_x;
 25608     }
 25609   SET_TEXT_POS (it->eol_pos, 0, 0);
 25610   ++it->vpos;
 25611   ++it->glyph_row;
 25612   /* The next row should by default use the same value of the
 25613      reversed_p flag as this one.  set_iterator_to_next decides when
 25614      it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
 25615      the flag accordingly.  */
 25616   if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
 25617     it->glyph_row->reversed_p = row->reversed_p;
 25618   it->start = row->end;
 25619   return MATRIX_ROW_DISPLAYS_TEXT_P (row);
 25620 
 25621 #undef RECORD_MAX_MIN_POS
 25622 }
 25623 
 25624 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
 25625        Scurrent_bidi_paragraph_direction, 0, 1, 0,
 25626        doc: /* Return paragraph direction at point in BUFFER.
 25627 Value is either `left-to-right' or `right-to-left'.
 25628 If BUFFER is omitted or nil, it defaults to the current buffer.
 25629 
 25630 Paragraph direction determines how the text in the paragraph is displayed.
 25631 In left-to-right paragraphs, text begins at the left margin of the window
 25632 and the reading direction is generally left to right.  In right-to-left
 25633 paragraphs, text begins at the right margin and is read from right to left.
 25634 
 25635 See also `bidi-paragraph-direction'.  */)
 25636   (Lisp_Object buffer)
 25637 {
 25638   struct buffer *buf = current_buffer;
 25639   struct buffer *old = buf;
 25640 
 25641   if (! NILP (buffer))
 25642     {
 25643       CHECK_BUFFER (buffer);
 25644       buf = XBUFFER (buffer);
 25645     }
 25646 
 25647   if (NILP (BVAR (buf, bidi_display_reordering))
 25648       || NILP (BVAR (buf, enable_multibyte_characters))
 25649       /* When we are loading loadup.el, the character property tables
 25650          needed for bidi iteration are not yet available.  */
 25651       || redisplay__inhibit_bidi)
 25652     return Qleft_to_right;
 25653   else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
 25654     return BVAR (buf, bidi_paragraph_direction);
 25655   else
 25656     {
 25657       /* Determine the direction from buffer text.  We could try to
 25658          use current_matrix if it is up to date, but this seems fast
 25659          enough as it is.  */
 25660       struct bidi_it itb;
 25661       ptrdiff_t pos = BUF_PT (buf);
 25662       ptrdiff_t bytepos = BUF_PT_BYTE (buf);
 25663       int c;
 25664       void *itb_data = bidi_shelve_cache ();
 25665 
 25666       set_buffer_temp (buf);
 25667       /* bidi_paragraph_init finds the base direction of the paragraph
 25668          by searching forward from paragraph start.  We need the base
 25669          direction of the current or _previous_ paragraph, so we need
 25670          to make sure we are within that paragraph.  To that end, find
 25671          the previous non-empty line.  */
 25672       if (pos >= ZV && pos > BEGV)
 25673         dec_both (&pos, &bytepos);
 25674       AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
 25675       if (fast_looking_at (trailing_white_space,
 25676                            pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
 25677         {
 25678           while ((c = FETCH_BYTE (bytepos)) == '\n'
 25679                  || c == ' ' || c == '\t' || c == '\f')
 25680             {
 25681               if (bytepos <= BEGV_BYTE)
 25682                 break;
 25683               bytepos--;
 25684               pos--;
 25685             }
 25686           while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
 25687             bytepos--;
 25688         }
 25689       bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
 25690       itb.paragraph_dir = NEUTRAL_DIR;
 25691       itb.string.s = NULL;
 25692       itb.string.lstring = Qnil;
 25693       itb.string.bufpos = 0;
 25694       itb.string.from_disp_str = false;
 25695       itb.string.unibyte = false;
 25696       /* We have no window to use here for ignoring window-specific
 25697          overlays.  Using NULL for window pointer will cause
 25698          compute_display_string_pos to use the current buffer.  */
 25699       itb.w = NULL;
 25700       bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
 25701       bidi_unshelve_cache (itb_data, false);
 25702       set_buffer_temp (old);
 25703       switch (itb.paragraph_dir)
 25704         {
 25705         case L2R:
 25706           return Qleft_to_right;
 25707           break;
 25708         case R2L:
 25709           return Qright_to_left;
 25710           break;
 25711         default:
 25712           emacs_abort ();
 25713         }
 25714     }
 25715 }
 25716 
 25717 DEFUN ("bidi-find-overridden-directionality",
 25718        Fbidi_find_overridden_directionality,
 25719        Sbidi_find_overridden_directionality, 3, 4, 0,
 25720        doc: /* Return position between FROM and TO where directionality was overridden.
 25721 
 25722 This function returns the first character position in the specified
 25723 region of OBJECT where characters have their bidirectional
 25724 properties affected in a way that might make its text look confusingly
 25725 on display.  For example, characters whose `bidi-class' property is `L',
 25726 could be forced to display as `R' by a directional override, and
 25727 likewise characters whose `bidi-class' is `R' or `AL' that are
 25728 forced to display as `L'.
 25729 
 25730 If no such character is found, the function returns nil.
 25731 
 25732 OBJECT is a Lisp string or buffer to search for overridden
 25733 directionality, and defaults to the current buffer if nil.
 25734 OBJECT can also be a window, in which case the function will search
 25735 the buffer displayed in that window.  Passing the window instead of
 25736 a buffer is preferable when the buffer is displayed in some window,
 25737 because this function will then be able to correctly account for
 25738 window-specific overlays, which can affect the results.
 25739 
 25740 Optional argument BASE-DIR specifies the base paragraph directory
 25741 of the text.  It should be a symbol, either `left-to-right'
 25742 or `right-to-left', and defaults to `left-to-right'.
 25743 
 25744 Strong directional characters `L', `R', and `AL' can have their
 25745 intrinsic directionality overridden by directional override control
 25746 characters RLO (u+202E) and LRO (u+202D).  They can also have their
 25747 directionality affected by other formatting control characters: LRE
 25748 (u+202A), RLE (u+202B), LRI (u+2066), and RLI (u+2067).  See the
 25749 function `get-char-code-property' for a way to inquire about the
 25750 `bidi-class' property of a character.  Characters whose intrinsic
 25751 directionality is weak or neutral, such as numbers or punctuation
 25752 characters, can be forced to display in a very different place with
 25753 respect of its surrounding characters, so as to make the surrounding
 25754 text confuse the user regarding what the text says.
 25755 
 25756 Also see the `highlight-confusing-reorderings' function, which can be
 25757 useful in similar circumstances as this function.  */)
 25758   (Lisp_Object from, Lisp_Object to, Lisp_Object object, Lisp_Object base_dir)
 25759 {
 25760   struct buffer *buf = current_buffer;
 25761   struct buffer *old = buf;
 25762   struct window *w = NULL;
 25763   bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
 25764   struct bidi_it itb;
 25765   ptrdiff_t from_pos, to_pos, from_bpos;
 25766   void *itb_data;
 25767 
 25768   if (!NILP (object))
 25769     {
 25770       if (BUFFERP (object))
 25771         buf = XBUFFER (object);
 25772       else if (WINDOWP (object))
 25773         {
 25774           w = decode_live_window (object);
 25775           buf = XBUFFER (w->contents);
 25776           frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
 25777         }
 25778       else
 25779         CHECK_STRING (object);
 25780     }
 25781 
 25782   if (STRINGP (object))
 25783     {
 25784       /* Characters in unibyte strings are always treated by bidi.c as
 25785          strong LTR.  */
 25786       if (!STRING_MULTIBYTE (object)
 25787           /* When we are loading loadup.el, the character property
 25788              tables needed for bidi iteration are not yet
 25789              available.  */
 25790           || redisplay__inhibit_bidi)
 25791         return Qnil;
 25792 
 25793       validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
 25794       if (from_pos >= SCHARS (object))
 25795         return Qnil;
 25796 
 25797       /* Set up the bidi iterator.  */
 25798       itb_data = bidi_shelve_cache ();
 25799       itb.paragraph_dir = NEUTRAL_DIR;
 25800       itb.string.lstring = object;
 25801       itb.string.s = NULL;
 25802       itb.string.schars = SCHARS (object);
 25803       itb.string.bufpos = 0;
 25804       itb.string.from_disp_str = false;
 25805       itb.string.unibyte = false;
 25806       itb.w = w;
 25807       bidi_init_it (0, 0, frame_window_p, &itb);
 25808     }
 25809   else
 25810     {
 25811       /* Nothing this fancy can happen in unibyte buffers, or in a
 25812          buffer that disabled reordering, or if FROM is at EOB.  */
 25813       if (NILP (BVAR (buf, bidi_display_reordering))
 25814           || NILP (BVAR (buf, enable_multibyte_characters))
 25815           /* When we are loading loadup.el, the character property
 25816              tables needed for bidi iteration are not yet
 25817              available.  */
 25818           || redisplay__inhibit_bidi)
 25819         return Qnil;
 25820 
 25821       set_buffer_temp (buf);
 25822       validate_region (&from, &to);
 25823       from_pos = XFIXNUM (from);
 25824       to_pos = XFIXNUM (to);
 25825       if (from_pos >= ZV)
 25826         return Qnil;
 25827 
 25828       /* Set up the bidi iterator.  */
 25829       itb_data = bidi_shelve_cache ();
 25830       from_bpos = CHAR_TO_BYTE (from_pos);
 25831       if (from_pos == BEGV)
 25832         {
 25833           itb.charpos = BEGV;
 25834           itb.bytepos = BEGV_BYTE;
 25835         }
 25836       else if (FETCH_BYTE (from_bpos - 1) == '\n')
 25837         {
 25838           itb.charpos = from_pos;
 25839           itb.bytepos = from_bpos;
 25840         }
 25841       else
 25842         itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
 25843                                             -1, &itb.bytepos);
 25844       itb.paragraph_dir = NEUTRAL_DIR;
 25845       itb.string.s = NULL;
 25846       itb.string.lstring = Qnil;
 25847       itb.string.bufpos = 0;
 25848       itb.string.from_disp_str = false;
 25849       itb.string.unibyte = false;
 25850       itb.w = w;
 25851       bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
 25852     }
 25853 
 25854   ptrdiff_t found;
 25855   bidi_dir_t bdir = EQ (base_dir, Qright_to_left) ? R2L : L2R;
 25856   do {
 25857     bidi_paragraph_init (bdir, &itb, false);
 25858     while ((found = bidi_find_first_overridden (&itb)) < from_pos)
 25859       ;
 25860   } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
 25861 
 25862   bidi_unshelve_cache (itb_data, false);
 25863   set_buffer_temp (old);
 25864 
 25865   return (from_pos <= found && found < to_pos) ? make_fixnum (found) : Qnil;
 25866 }
 25867 
 25868 DEFUN ("move-point-visually", Fmove_point_visually,
 25869        Smove_point_visually, 1, 1, 0,
 25870        doc: /* Move point in the visual order in the specified DIRECTION.
 25871 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
 25872 left.
 25873 
 25874 Value is the new character position of point.  */)
 25875   (Lisp_Object direction)
 25876 {
 25877   struct window *w = XWINDOW (selected_window);
 25878   struct buffer *b = XBUFFER (w->contents);
 25879   struct glyph_row *row;
 25880   int dir;
 25881   Lisp_Object paragraph_dir;
 25882 
 25883 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH)          \
 25884   (!(ROW)->continued_p                          \
 25885    && NILP ((GLYPH)->object)                    \
 25886    && (GLYPH)->type == CHAR_GLYPH               \
 25887    && (GLYPH)->u.ch == ' '                      \
 25888    && (GLYPH)->charpos >= 0                     \
 25889    && !(GLYPH)->avoid_cursor_p)
 25890 
 25891   CHECK_FIXNUM (direction);
 25892   dir = XFIXNUM (direction);
 25893   if (dir > 0)
 25894     dir = 1;
 25895   else
 25896     dir = -1;
 25897 
 25898   /* If current matrix is up-to-date, we can use the information
 25899      recorded in the glyphs, at least as long as the goal is on the
 25900      screen.  */
 25901   if (w->window_end_valid
 25902       && !windows_or_buffers_changed
 25903       && b
 25904       && !b->clip_changed
 25905       && !b->prevent_redisplay_optimizations_p
 25906       && !window_outdated (w)
 25907       /* We rely below on the cursor coordinates to be up to date, but
 25908          we cannot trust them if some command moved point since the
 25909          last complete redisplay.  */
 25910       && w->last_point == BUF_PT (b)
 25911       && w->cursor.vpos >= 0
 25912       && w->cursor.vpos < w->current_matrix->nrows
 25913       && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
 25914     {
 25915       struct glyph *g = row->glyphs[TEXT_AREA];
 25916       struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
 25917       struct glyph *gpt = g + w->cursor.hpos;
 25918 
 25919       for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
 25920         {
 25921           if (BUFFERP (g->object) && g->charpos != PT)
 25922             {
 25923               SET_PT (g->charpos);
 25924               w->cursor.vpos = -1;
 25925               return make_fixnum (PT);
 25926             }
 25927           else if (!NILP (g->object) && !EQ (g->object, gpt->object))
 25928             {
 25929               ptrdiff_t new_pos;
 25930 
 25931               if (BUFFERP (gpt->object))
 25932                 {
 25933                   new_pos = PT;
 25934                   if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
 25935                     new_pos += (row->reversed_p ? -dir : dir);
 25936                   else
 25937                     new_pos -= (row->reversed_p ? -dir : dir);
 25938                   new_pos = clip_to_bounds (BEGV, new_pos, ZV);
 25939                   /* If we didn't move, we've hit BEGV or ZV, so we
 25940                      need to signal a suitable error.  */
 25941                   if (new_pos == PT)
 25942                     break;
 25943                 }
 25944               else if (BUFFERP (g->object))
 25945                 new_pos = g->charpos;
 25946               else
 25947                 break;
 25948               SET_PT (new_pos);
 25949               w->cursor.vpos = -1;
 25950               return make_fixnum (PT);
 25951             }
 25952           else if (ROW_GLYPH_NEWLINE_P (row, g))
 25953             {
 25954               /* Glyphs inserted at the end of a non-empty line for
 25955                  positioning the cursor have zero charpos, so we must
 25956                  deduce the value of point by other means.  */
 25957               if (g->charpos > 0)
 25958                 SET_PT (g->charpos);
 25959               else if (row->ends_at_zv_p && PT != ZV)
 25960                 SET_PT (ZV);
 25961               else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
 25962                 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 25963               else
 25964                 break;
 25965               w->cursor.vpos = -1;
 25966               return make_fixnum (PT);
 25967             }
 25968         }
 25969       if (g == e || NILP (g->object))
 25970         {
 25971           if (row->truncated_on_left_p || row->truncated_on_right_p)
 25972             goto simulate_display;
 25973           if (!row->reversed_p)
 25974             row += dir;
 25975           else
 25976             row -= dir;
 25977           if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
 25978                 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
 25979             goto simulate_display;
 25980 
 25981           if (dir > 0)
 25982             {
 25983               if (row->reversed_p && !row->continued_p)
 25984                 {
 25985                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 25986                   w->cursor.vpos = -1;
 25987                   return make_fixnum (PT);
 25988                 }
 25989               g = row->glyphs[TEXT_AREA];
 25990               e = g + row->used[TEXT_AREA];
 25991               for ( ; g < e; g++)
 25992                 {
 25993                   if (BUFFERP (g->object)
 25994                       /* Empty lines have only one glyph, which stands
 25995                          for the newline, and whose charpos is the
 25996                          buffer position of the newline.  */
 25997                       || ROW_GLYPH_NEWLINE_P (row, g)
 25998                       /* When the buffer ends in a newline, the line at
 25999                          EOB also has one glyph, but its charpos is -1.  */
 26000                       || (row->ends_at_zv_p
 26001                           && !row->reversed_p
 26002                           && NILP (g->object)
 26003                           && g->type == CHAR_GLYPH
 26004                           && g->u.ch == ' '))
 26005                     {
 26006                       if (g->charpos > 0)
 26007                         SET_PT (g->charpos);
 26008                       else if (!row->reversed_p
 26009                                && row->ends_at_zv_p
 26010                                && PT != ZV)
 26011                         SET_PT (ZV);
 26012                       else
 26013                         continue;
 26014                       w->cursor.vpos = -1;
 26015                       return make_fixnum (PT);
 26016                     }
 26017                 }
 26018             }
 26019           else
 26020             {
 26021               if (!row->reversed_p && !row->continued_p)
 26022                 {
 26023                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26024                   w->cursor.vpos = -1;
 26025                   return make_fixnum (PT);
 26026                 }
 26027               e = row->glyphs[TEXT_AREA];
 26028               g = e + row->used[TEXT_AREA] - 1;
 26029               for ( ; g >= e; g--)
 26030                 {
 26031                   if (BUFFERP (g->object)
 26032                       || (ROW_GLYPH_NEWLINE_P (row, g)
 26033                           && g->charpos > 0)
 26034                       /* Empty R2L lines on GUI frames have the buffer
 26035                          position of the newline stored in the stretch
 26036                          glyph.  */
 26037                       || g->type == STRETCH_GLYPH
 26038                       || (row->ends_at_zv_p
 26039                           && row->reversed_p
 26040                           && NILP (g->object)
 26041                           && g->type == CHAR_GLYPH
 26042                           && g->u.ch == ' '))
 26043                     {
 26044                       if (g->charpos > 0)
 26045                         SET_PT (g->charpos);
 26046                       else if (row->reversed_p
 26047                                && row->ends_at_zv_p
 26048                                && PT != ZV)
 26049                         SET_PT (ZV);
 26050                       else
 26051                         continue;
 26052                       w->cursor.vpos = -1;
 26053                       return make_fixnum (PT);
 26054                     }
 26055                 }
 26056             }
 26057         }
 26058     }
 26059 
 26060  simulate_display:
 26061 
 26062   /* If we wind up here, we failed to move by using the glyphs, so we
 26063      need to simulate display instead.  */
 26064 
 26065   if (b)
 26066     paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
 26067   else
 26068     paragraph_dir = Qleft_to_right;
 26069   if (EQ (paragraph_dir, Qright_to_left))
 26070     dir = -dir;
 26071   if (PT <= BEGV && dir < 0)
 26072     xsignal0 (Qbeginning_of_buffer);
 26073   else if (PT >= ZV && dir > 0)
 26074     xsignal0 (Qend_of_buffer);
 26075   else
 26076     {
 26077       struct text_pos pt;
 26078       struct it it;
 26079       int pt_x, target_x, pixel_width, pt_vpos;
 26080       bool at_eol_p;
 26081       bool overshoot_expected = false;
 26082       bool target_is_eol_p = false;
 26083       void *itdata = bidi_shelve_cache ();
 26084 
 26085       /* Setup the arena.  */
 26086       SET_TEXT_POS (pt, PT, PT_BYTE);
 26087       start_display (&it, w, pt);
 26088       /* When lines are truncated, we could be called with point
 26089          outside of the windows edges, in which case move_it_*
 26090          functions either prematurely stop at window's edge or jump to
 26091          the next screen line, whereas we rely below on our ability to
 26092          reach point, in order to start from its X coordinate.  So we
 26093          need to disregard the window's horizontal extent in that case.  */
 26094       if (it.line_wrap == TRUNCATE)
 26095         it.last_visible_x = DISP_INFINITY;
 26096 
 26097       if (it.cmp_it.id < 0
 26098           && it.method == GET_FROM_STRING
 26099           && it.area == TEXT_AREA
 26100           && it.string_from_display_prop_p
 26101           && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
 26102         overshoot_expected = true;
 26103 
 26104       /* Find the X coordinate of point.  We start from the beginning
 26105          of this or previous line to make sure we are before point in
 26106          the logical order (since the move_it_* functions can only
 26107          move forward).  */
 26108     reseat:
 26109       reseat_at_previous_visible_line_start (&it);
 26110       it.current_x = it.hpos = it.current_y = it.vpos = 0;
 26111       if (IT_CHARPOS (it) != PT)
 26112         {
 26113           move_it_to (&it, overshoot_expected ? PT - 1 : PT,
 26114                       -1, -1, -1, MOVE_TO_POS);
 26115           /* If we missed point because the character there is
 26116              displayed out of a display vector that has more than one
 26117              glyph, retry expecting overshoot.  */
 26118           if (it.method == GET_FROM_DISPLAY_VECTOR
 26119               && it.current.dpvec_index > 0
 26120               && !overshoot_expected)
 26121             {
 26122               overshoot_expected = true;
 26123               goto reseat;
 26124             }
 26125           else if (IT_CHARPOS (it) != PT && !overshoot_expected)
 26126             move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
 26127         }
 26128       pt_x = it.current_x;
 26129       pt_vpos = it.vpos;
 26130       if (dir > 0 || overshoot_expected)
 26131         {
 26132           struct glyph_row *row = it.glyph_row;
 26133 
 26134           /* When point is at beginning of line, we don't have
 26135              information about the glyph there loaded into struct
 26136              it.  Calling get_next_display_element fixes that.  */
 26137           if (pt_x == 0)
 26138             get_next_display_element (&it);
 26139           at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26140           it.glyph_row = NULL;
 26141           PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
 26142           it.glyph_row = row;
 26143           /* PRODUCE_GLYPHS advances it.current_x, so we must restore
 26144              it, lest it will become out of sync with it's buffer
 26145              position.  */
 26146           it.current_x = pt_x;
 26147         }
 26148       else
 26149         at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26150       pixel_width = it.pixel_width;
 26151       if (overshoot_expected && at_eol_p)
 26152         pixel_width = 0;
 26153       else if (pixel_width <= 0)
 26154         pixel_width = 1;
 26155 
 26156       /* If there's a display string (or something similar) at point,
 26157          we are actually at the glyph to the left of point, so we need
 26158          to correct the X coordinate.  */
 26159       if (overshoot_expected)
 26160         {
 26161           if (it.bidi_p)
 26162             pt_x += pixel_width * it.bidi_it.scan_dir;
 26163           else
 26164             pt_x += pixel_width;
 26165         }
 26166 
 26167       /* Compute target X coordinate, either to the left or to the
 26168          right of point.  On TTY frames, all characters have the same
 26169          pixel width of 1, so we can use that.  On GUI frames we don't
 26170          have an easy way of getting at the pixel width of the
 26171          character to the left of point, so we use a different method
 26172          of getting to that place.  */
 26173       if (dir > 0)
 26174         target_x = pt_x + pixel_width;
 26175       else
 26176         target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
 26177 
 26178       /* Target X coordinate could be one line above or below the line
 26179          of point, in which case we need to adjust the target X
 26180          coordinate.  Also, if moving to the left, we need to begin at
 26181          the left edge of the point's screen line.  */
 26182       if (dir < 0)
 26183         {
 26184           if (pt_x > 0)
 26185             {
 26186               start_display (&it, w, pt);
 26187               if (it.line_wrap == TRUNCATE)
 26188                 it.last_visible_x = DISP_INFINITY;
 26189               reseat_at_previous_visible_line_start (&it);
 26190               it.current_x = it.current_y = it.hpos = 0;
 26191               if (pt_vpos != 0)
 26192                 move_it_by_lines (&it, pt_vpos);
 26193             }
 26194           else
 26195             {
 26196               move_it_by_lines (&it, -1);
 26197               target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
 26198               target_is_eol_p = true;
 26199               /* Under word-wrap, we don't know the x coordinate of
 26200                  the last character displayed on the previous line,
 26201                  which immediately precedes the wrap point.  To find
 26202                  out its x coordinate, we try moving to the right
 26203                  margin of the window, which will stop at the wrap
 26204                  point, and then reset target_x to point at the
 26205                  character that precedes the wrap point.  This is not
 26206                  needed on GUI frames, because (see below) there we
 26207                  move from the left margin one grapheme cluster at a
 26208                  time, and stop when we hit the wrap point.  */
 26209               if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
 26210                 {
 26211                   void *it_data = NULL;
 26212                   struct it it2;
 26213 
 26214                   SAVE_IT (it2, it, it_data);
 26215                   move_it_in_display_line_to (&it, ZV, target_x,
 26216                                               MOVE_TO_POS | MOVE_TO_X);
 26217                   /* If we arrived at target_x, that _is_ the last
 26218                      character on the previous line.  */
 26219                   if (it.current_x != target_x)
 26220                     target_x = it.current_x - 1;
 26221                   RESTORE_IT (&it, &it2, it_data);
 26222                 }
 26223             }
 26224         }
 26225       else
 26226         {
 26227           if (at_eol_p
 26228               || (target_x >= it.last_visible_x
 26229                   && it.line_wrap != TRUNCATE))
 26230             {
 26231               if (pt_x > 0)
 26232                 move_it_by_lines (&it, 0);
 26233               move_it_by_lines (&it, 1);
 26234               target_x = 0;
 26235             }
 26236         }
 26237 
 26238       /* Move to the target X coordinate.  */
 26239       /* On GUI frames, as we don't know the X coordinate of the
 26240          character to the left of point, moving point to the left
 26241          requires walking, one grapheme cluster at a time, until we
 26242          find ourself at a place immediately to the left of the
 26243          character at point.  */
 26244       if (FRAME_WINDOW_P (it.f) && dir < 0)
 26245         {
 26246           struct text_pos new_pos;
 26247           enum move_it_result rc = MOVE_X_REACHED;
 26248 
 26249           if (it.current_x == 0)
 26250             get_next_display_element (&it);
 26251           if (it.what == IT_COMPOSITION)
 26252             {
 26253               new_pos.charpos = it.cmp_it.charpos;
 26254               new_pos.bytepos = -1;
 26255             }
 26256           else
 26257             new_pos = it.current.pos;
 26258 
 26259           while (it.current_x + it.pixel_width <= target_x
 26260                  && (rc == MOVE_X_REACHED
 26261                      /* Under word-wrap, move_it_in_display_line_to
 26262                         stops at correct coordinates, but sometimes
 26263                         returns MOVE_POS_MATCH_OR_ZV.  */
 26264                      || (it.line_wrap == WORD_WRAP
 26265                          && rc == MOVE_POS_MATCH_OR_ZV)))
 26266             {
 26267               int new_x = it.current_x + it.pixel_width;
 26268 
 26269               /* For composed characters, we want the position of the
 26270                  first character in the grapheme cluster (usually, the
 26271                  composition's base character), whereas it.current
 26272                  might give us the position of the _last_ one, e.g. if
 26273                  the composition is rendered in reverse due to bidi
 26274                  reordering.  */
 26275               if (it.what == IT_COMPOSITION)
 26276                 {
 26277                   new_pos.charpos = it.cmp_it.charpos;
 26278                   new_pos.bytepos = -1;
 26279                 }
 26280               else
 26281                 new_pos = it.current.pos;
 26282               if (new_x == it.current_x)
 26283                 new_x++;
 26284               rc = move_it_in_display_line_to (&it, ZV, new_x,
 26285                                                MOVE_TO_POS | MOVE_TO_X);
 26286               if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
 26287                 break;
 26288             }
 26289           /* The previous position we saw in the loop is the one we
 26290              want.  */
 26291           if (new_pos.bytepos == -1)
 26292             new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
 26293           it.current.pos = new_pos;
 26294         }
 26295       else if (it.current_x != target_x)
 26296         move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
 26297 
 26298       /* If we ended up in a display string that covers point, move to
 26299          buffer position to the right in the visual order.  */
 26300       if (dir > 0)
 26301         {
 26302           while (IT_CHARPOS (it) == PT)
 26303             {
 26304               set_iterator_to_next (&it, false);
 26305               if (!get_next_display_element (&it))
 26306                 break;
 26307             }
 26308         }
 26309 
 26310       /* Move point to that position.  */
 26311       SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
 26312       bidi_unshelve_cache (itdata, false);
 26313     }
 26314 
 26315   return make_fixnum (PT);
 26316 
 26317 #undef ROW_GLYPH_NEWLINE_P
 26318 }
 26319 
 26320 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
 26321        Sbidi_resolved_levels, 0, 1, 0,
 26322        doc: /* Return the resolved bidirectional levels of characters at VPOS.
 26323 
 26324 The resolved levels are produced by the Emacs bidi reordering engine
 26325 that implements the UBA, the Unicode Bidirectional Algorithm.  Please
 26326 read the Unicode Standard Annex 9 (UAX#9) for background information
 26327 about these levels.
 26328 
 26329 VPOS is the zero-based number of the current window's screen line
 26330 for which to produce the resolved levels.  If VPOS is nil or omitted,
 26331 it defaults to the screen line of point.  If the window displays a
 26332 header line, VPOS of zero will report on the header line, and first
 26333 line of text in the window will have VPOS of 1.
 26334 
 26335 Value is an array of resolved levels, indexed by glyph number.
 26336 Glyphs are numbered from zero starting from the beginning of the
 26337 screen line, i.e. the left edge of the window for left-to-right lines
 26338 and from the right edge for right-to-left lines.  The resolved levels
 26339 are produced only for the window's text area; text in display margins
 26340 is not included.
 26341 
 26342 If the selected window's display is not up-to-date, or if the specified
 26343 screen line does not display text, this function returns nil.  It is
 26344 highly recommended to bind this function to some simple key, like F8,
 26345 in order to avoid these problems.
 26346 
 26347 This function exists mainly for testing the correctness of the
 26348 Emacs UBA implementation, in particular with the test suite.  */)
 26349   (Lisp_Object vpos)
 26350 {
 26351   struct window *w = XWINDOW (selected_window);
 26352   struct buffer *b = XBUFFER (w->contents);
 26353   int nrow;
 26354   struct glyph_row *row;
 26355 
 26356   if (NILP (vpos))
 26357     {
 26358       int d1, d2, d3, d4, d5;
 26359 
 26360       pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
 26361     }
 26362   else
 26363     {
 26364       CHECK_FIXNUM (vpos);
 26365       nrow = XFIXNUM (vpos);
 26366     }
 26367 
 26368   /* We require up-to-date glyph matrix for this window.  */
 26369   if (w->window_end_valid
 26370       && !windows_or_buffers_changed
 26371       && b
 26372       && !b->clip_changed
 26373       && !b->prevent_redisplay_optimizations_p
 26374       && !window_outdated (w)
 26375       && nrow >= 0
 26376       && nrow < w->current_matrix->nrows
 26377       && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
 26378       && MATRIX_ROW_DISPLAYS_TEXT_P (row))
 26379     {
 26380       struct glyph *g, *e, *g1;
 26381       int nglyphs, i;
 26382       Lisp_Object levels;
 26383 
 26384       if (!row->reversed_p)     /* Left-to-right glyph row.  */
 26385         {
 26386           g = g1 = row->glyphs[TEXT_AREA];
 26387           e = g + row->used[TEXT_AREA];
 26388 
 26389           /* Skip over glyphs at the start of the row that was
 26390              generated by redisplay for its own needs.  */
 26391           while (g < e
 26392                  && NILP (g->object)
 26393                  && g->charpos < 0)
 26394             g++;
 26395           g1 = g;
 26396 
 26397           /* Count the "interesting" glyphs in this row.  */
 26398           for (nglyphs = 0; g < e && !NILP (g->object); g++)
 26399             nglyphs++;
 26400 
 26401           /* Create and fill the array.  */
 26402           levels = make_uninit_vector (nglyphs);
 26403           for (i = 0; g1 < g; i++, g1++)
 26404             ASET (levels, i, make_fixnum (g1->resolved_level));
 26405         }
 26406       else                      /* Right-to-left glyph row.  */
 26407         {
 26408           g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 26409           e = row->glyphs[TEXT_AREA] - 1;
 26410           while (g > e
 26411                  && NILP (g->object)
 26412                  && g->charpos < 0)
 26413             g--;
 26414           g1 = g;
 26415           for (nglyphs = 0; g > e && !NILP (g->object); g--)
 26416             nglyphs++;
 26417           levels = make_uninit_vector (nglyphs);
 26418           for (i = 0; g1 > g; i++, g1--)
 26419             ASET (levels, i, make_fixnum (g1->resolved_level));
 26420         }
 26421       return levels;
 26422     }
 26423   else
 26424     return Qnil;
 26425 }
 26426 
 26427 
 26428 
 26429 /***********************************************************************
 26430                                Menu Bar
 26431  ***********************************************************************/
 26432 
 26433 /* Redisplay the menu bar in the frame for window W.
 26434 
 26435    The menu bar of X frames that don't have X toolkit support is
 26436    displayed in a special window W->frame->menu_bar_window.
 26437 
 26438    The menu bar of terminal frames is treated specially as far as
 26439    glyph matrices are concerned.  Menu bar lines are not part of
 26440    windows, so the update is done directly on the frame matrix rows
 26441    for the menu bar.  */
 26442 
 26443 static void
 26444 display_menu_bar (struct window *w)
 26445 {
 26446   struct frame *f = XFRAME (WINDOW_FRAME (w));
 26447   struct it it;
 26448   Lisp_Object items;
 26449   int i;
 26450 
 26451   /* Don't do all this for graphical frames.  */
 26452 #ifdef HAVE_NTGUI
 26453   if (FRAME_W32_P (f))
 26454     return;
 26455 #endif
 26456 #if defined (HAVE_PGTK)
 26457   if (FRAME_PGTK_P (f))
 26458     return;
 26459 #endif
 26460 
 26461 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26462   if (FRAME_X_P (f))
 26463     return;
 26464 #endif
 26465 
 26466 #ifdef HAVE_NS
 26467   if (FRAME_NS_P (f))
 26468     return;
 26469 #endif /* HAVE_NS */
 26470 
 26471 #ifdef HAVE_HAIKU
 26472   if (FRAME_HAIKU_P (f))
 26473     return;
 26474 #endif /* HAVE_HAIKU */
 26475 
 26476 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26477   eassert (!FRAME_WINDOW_P (f));
 26478   init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
 26479   it.first_visible_x = 0;
 26480   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26481 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 26482   if (FRAME_WINDOW_P (f))
 26483     {
 26484       /* Menu bar lines are displayed in the desired matrix of the
 26485          dummy window menu_bar_window.  */
 26486       struct window *menu_w;
 26487       menu_w = XWINDOW (f->menu_bar_window);
 26488       init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
 26489                      MENU_FACE_ID);
 26490       it.first_visible_x = 0;
 26491       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26492     }
 26493   else
 26494 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 26495     {
 26496       /* This is a TTY frame, i.e. character hpos/vpos are used as
 26497          pixel x/y.  */
 26498       init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
 26499                      MENU_FACE_ID);
 26500       it.first_visible_x = 0;
 26501       it.last_visible_x = FRAME_COLS (f);
 26502     }
 26503 
 26504   /* FIXME: This should be controlled by a user option.  See the
 26505      comments in redisplay_tool_bar and display_mode_line about
 26506      this.  */
 26507   it.paragraph_embedding = L2R;
 26508 
 26509   /* Clear all rows of the menu bar.  */
 26510   for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
 26511     {
 26512       struct glyph_row *row = it.glyph_row + i;
 26513       clear_glyph_row (row);
 26514       row->enabled_p = true;
 26515       row->full_width_p = true;
 26516       row->reversed_p = false;
 26517     }
 26518 
 26519   /* Display all items of the menu bar.  */
 26520   items = FRAME_MENU_BAR_ITEMS (it.f);
 26521   for (i = 0; i < ASIZE (items); i += 4)
 26522     {
 26523       Lisp_Object string;
 26524 
 26525       /* Stop at nil string.  */
 26526       string = AREF (items, i + 1);
 26527       if (NILP (string))
 26528         break;
 26529 
 26530       /* Remember where item was displayed.  */
 26531       ASET (items, i + 3, make_fixnum (it.hpos));
 26532 
 26533       /* Display the item, pad with one space.  */
 26534       if (it.current_x < it.last_visible_x)
 26535         display_string (NULL, string, Qnil, 0, 0, &it,
 26536                         SCHARS (string) + 1, 0, 0, STRING_MULTIBYTE (string));
 26537     }
 26538 
 26539   /* Fill out the line with spaces.  */
 26540   if (it.current_x < it.last_visible_x)
 26541     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 26542 
 26543   /* Compute the total height of the lines.  */
 26544   compute_line_metrics (&it);
 26545 }
 26546 
 26547 /* Deep copy of a glyph row, including the glyphs.  */
 26548 static void
 26549 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
 26550 {
 26551   struct glyph *pointers[1 + LAST_AREA];
 26552   int to_used = to->used[TEXT_AREA];
 26553 
 26554   /* Save glyph pointers of TO.  */
 26555   memcpy (pointers, to->glyphs, sizeof to->glyphs);
 26556 
 26557   /* Do a structure assignment.  */
 26558   *to = *from;
 26559 
 26560   /* Restore original glyph pointers of TO.  */
 26561   memcpy (to->glyphs, pointers, sizeof to->glyphs);
 26562 
 26563   /* Copy the glyphs.  */
 26564   memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
 26565           min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
 26566 
 26567   /* If we filled only part of the TO row, fill the rest with
 26568      space_glyph (which will display as empty space).  */
 26569   if (to_used > from->used[TEXT_AREA])
 26570     fill_up_frame_row_with_spaces (to, to_used);
 26571 }
 26572 
 26573 /* Display one menu item on a TTY, by overwriting the glyphs in the
 26574    frame F's desired glyph matrix with glyphs produced from the menu
 26575    item text.  Called from term.c to display TTY drop-down menus one
 26576    item at a time.
 26577 
 26578    ITEM_TEXT is the menu item text as a C string.
 26579 
 26580    FACE_ID is the face ID to be used for this menu item.  FACE_ID
 26581    could specify one of 3 faces: a face for an enabled item, a face
 26582    for a disabled item, or a face for a selected item.
 26583 
 26584    X and Y are coordinates of the first glyph in the frame's desired
 26585    matrix to be overwritten by the menu item.  Since this is a TTY, Y
 26586    is the zero-based number of the glyph row and X is the zero-based
 26587    glyph number in the row, starting from left, where to start
 26588    displaying the item.
 26589 
 26590    SUBMENU means this menu item drops down a submenu, which
 26591    should be indicated by displaying a proper visual cue after the
 26592    item text.  */
 26593 
 26594 void
 26595 display_tty_menu_item (const char *item_text, int width, int face_id,
 26596                        int x, int y, bool submenu)
 26597 {
 26598   struct it it;
 26599   struct frame *f = SELECTED_FRAME ();
 26600   struct window *w = XWINDOW (f->selected_window);
 26601   struct glyph_row *row;
 26602   size_t item_len = strlen (item_text);
 26603 
 26604   eassert (FRAME_TERMCAP_P (f));
 26605 
 26606   /* Don't write beyond the matrix's last row.  This can happen for
 26607      TTY screens that are not high enough to show the entire menu.
 26608      (This is actually a bit of defensive programming, as
 26609      tty_menu_display already limits the number of menu items to one
 26610      less than the number of screen lines.)  */
 26611   if (y >= f->desired_matrix->nrows)
 26612     return;
 26613 
 26614   init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
 26615   it.first_visible_x = 0;
 26616   it.last_visible_x = FRAME_COLS (f) - 1;
 26617   row = it.glyph_row;
 26618   /* Start with the row contents from the current matrix.  */
 26619   deep_copy_glyph_row (row, f->current_matrix->rows + y);
 26620   bool saved_width = row->full_width_p;
 26621   row->full_width_p = true;
 26622   bool saved_reversed = row->reversed_p;
 26623   row->reversed_p = false;
 26624   row->enabled_p = true;
 26625 
 26626   /* Arrange for the menu item glyphs to start at (X,Y) and have the
 26627      desired face.  */
 26628   eassert (x < f->desired_matrix->matrix_w);
 26629   it.current_x = it.hpos = x;
 26630   it.current_y = it.vpos = y;
 26631   int saved_used = row->used[TEXT_AREA];
 26632   bool saved_truncated = row->truncated_on_right_p;
 26633   row->used[TEXT_AREA] = x;
 26634   it.face_id = face_id;
 26635   it.line_wrap = TRUNCATE;
 26636 
 26637   /* FIXME: This should be controlled by a user option.  See the
 26638      comments in redisplay_tool_bar and display_mode_line about this.
 26639      Also, if paragraph_embedding could ever be R2L, changes will be
 26640      needed to avoid shifting to the right the row characters in
 26641      term.c:append_glyph.  */
 26642   it.paragraph_embedding = L2R;
 26643 
 26644   /* Pad with a space on the left.  */
 26645   display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
 26646   width--;
 26647   /* Display the menu item, pad with spaces to WIDTH.  */
 26648   if (submenu)
 26649     {
 26650       display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26651                       item_len, 0, FRAME_COLS (f) - 1, -1);
 26652       width -= item_len;
 26653       /* Indicate with " >" that there's a submenu.  */
 26654       display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
 26655                       FRAME_COLS (f) - 1, -1);
 26656     }
 26657   else
 26658     display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26659                     width, 0, FRAME_COLS (f) - 1, -1);
 26660 
 26661   row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
 26662   row->truncated_on_right_p = saved_truncated;
 26663   row->hash = row_hash (row);
 26664   row->full_width_p = saved_width;
 26665   row->reversed_p = saved_reversed;
 26666 }
 26667 
 26668 /***********************************************************************
 26669                               Mode Line
 26670  ***********************************************************************/
 26671 
 26672 /* Redisplay mode lines in the window tree whose root is WINDOW.
 26673    If FORCE, redisplay mode lines unconditionally.
 26674    Otherwise, redisplay only mode lines that are garbaged.  Value is
 26675    the number of windows whose mode lines were redisplayed.  */
 26676 
 26677 static int
 26678 redisplay_mode_lines (Lisp_Object window, bool force)
 26679 {
 26680   int nwindows = 0;
 26681 
 26682   while (!NILP (window))
 26683     {
 26684       struct window *w = XWINDOW (window);
 26685 
 26686       if (WINDOWP (w->contents))
 26687         nwindows += redisplay_mode_lines (w->contents, force);
 26688       else if (force
 26689                || FRAME_GARBAGED_P (XFRAME (w->frame))
 26690                || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
 26691         {
 26692           struct text_pos lpoint;
 26693           struct buffer *old = current_buffer;
 26694 
 26695           /* Set the window's buffer for the mode line display.  */
 26696           SET_TEXT_POS (lpoint, PT, PT_BYTE);
 26697           set_buffer_internal_1 (XBUFFER (w->contents));
 26698 
 26699           /* Point refers normally to the selected window.  For any
 26700              other window, set up appropriate value.  */
 26701           if (!EQ (window, selected_window))
 26702             {
 26703               struct text_pos pt;
 26704 
 26705               CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
 26706               TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
 26707             }
 26708 
 26709           /* Display mode lines.  */
 26710           clear_glyph_matrix (w->desired_matrix);
 26711           if (display_mode_lines (w))
 26712             ++nwindows;
 26713 
 26714           /* Restore old settings.  */
 26715           set_buffer_internal_1 (old);
 26716           TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 26717         }
 26718 
 26719       window = w->next;
 26720     }
 26721 
 26722   return nwindows;
 26723 }
 26724 
 26725 
 26726 /* Display the mode line, the header line, and the tab-line of window
 26727    W.  Value is the sum number of mode lines, header lines, and tab
 26728    lines actually displayed.  */
 26729 
 26730 static int
 26731 display_mode_lines (struct window *w)
 26732 {
 26733   Lisp_Object old_selected_window = selected_window;
 26734   Lisp_Object new_frame = w->frame;
 26735   specpdl_ref count = SPECPDL_INDEX ();
 26736   int n = 0;
 26737 
 26738   record_unwind_protect (restore_selected_window, selected_window);
 26739   record_unwind_protect
 26740     (restore_frame_selected_window, XFRAME (new_frame)->selected_window);
 26741 
 26742   if (window_wants_mode_line (w))
 26743     {
 26744       Lisp_Object window;
 26745       Lisp_Object default_help
 26746         = buffer_local_value (Qmode_line_default_help_echo, w->contents);
 26747 
 26748       /* Set up mode line help echo.  Do this before selecting w so it
 26749          can reasonably tell whether a mouse click will select w.  */
 26750       XSETWINDOW (window, w);
 26751       if (FUNCTIONP (default_help))
 26752         wset_mode_line_help_echo (w, safe_call1 (default_help, window));
 26753       else if (STRINGP (default_help))
 26754         wset_mode_line_help_echo (w, default_help);
 26755       else
 26756         wset_mode_line_help_echo (w, Qnil);
 26757     }
 26758 
 26759   selected_frame = new_frame;
 26760   /* FIXME: If we were to allow the mode-line's computation changing the buffer
 26761      or window's point, then we'd need select_window_1 here as well.  */
 26762   XSETWINDOW (selected_window, w);
 26763   XFRAME (new_frame)->selected_window = selected_window;
 26764 
 26765   /* These will be set while the mode line specs are processed.  */
 26766   line_number_displayed = false;
 26767   w->column_number_displayed = -1;
 26768 
 26769   if (window_wants_mode_line (w))
 26770     {
 26771       Lisp_Object window_mode_line_format
 26772         = window_parameter (w, Qmode_line_format);
 26773       struct window *sel_w = XWINDOW (old_selected_window);
 26774 
 26775       /* Select mode line face based on the real selected window.  */
 26776       display_mode_line (w,
 26777                          CURRENT_MODE_LINE_ACTIVE_FACE_ID_3 (sel_w, sel_w, w),
 26778                          NILP (window_mode_line_format)
 26779                          ? BVAR (current_buffer, mode_line_format)
 26780                          : window_mode_line_format);
 26781       ++n;
 26782     }
 26783 
 26784   if (window_wants_tab_line (w))
 26785     {
 26786       Lisp_Object window_tab_line_format
 26787         = window_parameter (w, Qtab_line_format);
 26788 
 26789       display_mode_line (w, TAB_LINE_FACE_ID,
 26790                          NILP (window_tab_line_format)
 26791                          ? BVAR (current_buffer, tab_line_format)
 26792                          : window_tab_line_format);
 26793       ++n;
 26794     }
 26795 
 26796   if (window_wants_header_line (w))
 26797     {
 26798       Lisp_Object window_header_line_format
 26799         = window_parameter (w, Qheader_line_format);
 26800 
 26801       display_mode_line (w, HEADER_LINE_FACE_ID,
 26802                          NILP (window_header_line_format)
 26803                          ? BVAR (current_buffer, header_line_format)
 26804                          : window_header_line_format);
 26805       ++n;
 26806     }
 26807 
 26808   unbind_to (count, Qnil);
 26809 
 26810   if (n > 0)
 26811     w->must_be_updated_p = true;
 26812   return n;
 26813 }
 26814 
 26815 
 26816 /* Display mode or header/tab line of window W.  FACE_ID specifies
 26817    which line to display; it is either MODE_LINE_ACTIVE_FACE_ID,
 26818    HEADER_LINE_FACE_ID or TAB_LINE_FACE_ID.  FORMAT is the
 26819    mode/header/tab line format to display.  Value is the pixel height
 26820    of the mode/header/tab line displayed.  */
 26821 
 26822 static int
 26823 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
 26824 {
 26825   struct it it;
 26826   struct face *face;
 26827   specpdl_ref count = SPECPDL_INDEX ();
 26828 
 26829   init_iterator (&it, w, -1, -1, NULL, face_id);
 26830   /* Don't extend on a previously drawn mode-line.
 26831      This may happen if called from pos_visible_p.  */
 26832   it.glyph_row->enabled_p = false;
 26833   prepare_desired_row (w, it.glyph_row, true);
 26834 
 26835   it.glyph_row->mode_line_p = true;
 26836   if (face_id == TAB_LINE_FACE_ID)
 26837     {
 26838       it.glyph_row->tab_line_p = true;
 26839       w->desired_matrix->tab_line_p = true;
 26840     }
 26841   else if (face_id == HEADER_LINE_FACE_ID)
 26842     w->desired_matrix->header_line_p = true;
 26843 
 26844   /* FIXME: This should be controlled by a user option.  But
 26845      supporting such an option is not trivial, since the mode line is
 26846      made up of many separate strings.  */
 26847   it.paragraph_embedding = L2R;
 26848 
 26849   record_unwind_protect (unwind_format_mode_line,
 26850                          format_mode_line_unwind_data (NULL, NULL,
 26851                                                        Qnil, false));
 26852 
 26853   /* Temporarily make frame's keyboard the current kboard so that
 26854      kboard-local variables in the mode_line_format will get the right
 26855      values.  */
 26856   push_kboard (FRAME_KBOARD (it.f));
 26857   record_unwind_save_match_data ();
 26858 
 26859   if (NILP (Vmode_line_compact)
 26860       || face_id == HEADER_LINE_FACE_ID || face_id == TAB_LINE_FACE_ID)
 26861     {
 26862       mode_line_target = MODE_LINE_DISPLAY;
 26863       display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 26864     }
 26865   else
 26866     {
 26867       Lisp_Object mode_string = Fformat_mode_line (format, Qnil, Qnil, Qnil);
 26868       if (EQ (Vmode_line_compact, Qlong)
 26869           && WINDOW_TOTAL_COLS (w) >= SCHARS (mode_string))
 26870         {
 26871           /* The window is wide enough; just display the mode line we
 26872              just computed. */
 26873           display_string (NULL, mode_string, Qnil,
 26874                           0, 0, &it, 0, 0, 0,
 26875                           STRING_MULTIBYTE (mode_string));
 26876         }
 26877       else
 26878         {
 26879           /* Compress the mode line. */
 26880           ptrdiff_t i = 0, i_byte = 0, start = 0;
 26881           int prev = 0;
 26882 
 26883           while (i < SCHARS (mode_string))
 26884             {
 26885               int c = fetch_string_char_advance (mode_string, &i, &i_byte);
 26886               if (c == ' ' && prev == ' ')
 26887                 {
 26888                   display_string (NULL,
 26889                                   Fsubstring (mode_string, make_fixnum (start),
 26890                                               make_fixnum (i - 1)),
 26891                                   Qnil, 0, 0, &it, 0, 0, 0,
 26892                                   STRING_MULTIBYTE (mode_string));
 26893                   /* Skip past the rest of the space characters. */
 26894                   while (c == ' ' && i < SCHARS (mode_string))
 26895                       c = fetch_string_char_advance (mode_string, &i, &i_byte);
 26896                   start = i - 1;
 26897                 }
 26898               prev = c;
 26899             }
 26900 
 26901           /* Display the final bit. */
 26902           if (start < i)
 26903             display_string (NULL,
 26904                             Fsubstring (mode_string, make_fixnum (start),
 26905                                         make_fixnum (i)),
 26906                             Qnil, 0, 0, &it, 0, 0, 0,
 26907                             STRING_MULTIBYTE (mode_string));
 26908         }
 26909     }
 26910   pop_kboard ();
 26911 
 26912   unbind_to (count, Qnil);
 26913 
 26914   /* Fill up with spaces.  */
 26915   display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
 26916 
 26917   compute_line_metrics (&it);
 26918   it.glyph_row->full_width_p = true;
 26919   it.glyph_row->continued_p = false;
 26920   it.glyph_row->truncated_on_left_p = false;
 26921   it.glyph_row->truncated_on_right_p = false;
 26922 
 26923   /* Make a 3D mode-line have a shadow at its right end.  */
 26924   face = FACE_FROM_ID (it.f, face_id);
 26925   extend_face_to_end_of_line (&it);
 26926   if (face->box != FACE_NO_BOX)
 26927     {
 26928       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 26929                             + it.glyph_row->used[TEXT_AREA] - 1);
 26930       int box_thickness = face->box_vertical_line_width;
 26931       last->right_box_line_p = true;
 26932       /* Add back the space for the right box line we subtracted in
 26933          init_iterator, since the right_box_line_p flag will make the
 26934          glyph wider.  We actually add only as much space as is
 26935          available for the last glyph of the modeline and whatever
 26936          space is left beyond it, since that glyph could be only
 26937          partially visible */
 26938       if (box_thickness > 0)
 26939         last->pixel_width += max (0, (box_thickness
 26940                                       - (it.current_x - it.last_visible_x)));
 26941     }
 26942 
 26943   return it.glyph_row->height;
 26944 }
 26945 
 26946 /* Move element ELT in LIST to the front of LIST.
 26947    Return the updated list.  */
 26948 
 26949 static Lisp_Object
 26950 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
 26951 {
 26952   register Lisp_Object tail, prev;
 26953   register Lisp_Object tem;
 26954 
 26955   tail = list;
 26956   prev = Qnil;
 26957   while (CONSP (tail))
 26958     {
 26959       tem = XCAR (tail);
 26960 
 26961       if (EQ (elt, tem))
 26962         {
 26963           /* Splice out the link TAIL.  */
 26964           if (NILP (prev))
 26965             list = XCDR (tail);
 26966           else
 26967             Fsetcdr (prev, XCDR (tail));
 26968 
 26969           /* Now make it the first.  */
 26970           Fsetcdr (tail, list);
 26971           return tail;
 26972         }
 26973       else
 26974         prev = tail;
 26975       tail = XCDR (tail);
 26976       maybe_quit ();
 26977     }
 26978 
 26979   /* Not found--return unchanged LIST.  */
 26980   return list;
 26981 }
 26982 
 26983 /* Subroutine to call Fset_text_properties through
 26984    internal_condition_case_n.  ARGS are the arguments of
 26985    Fset_text_properties, in order.  */
 26986 
 26987 static Lisp_Object
 26988 safe_set_text_properties (ptrdiff_t nargs, Lisp_Object *args)
 26989 {
 26990   eassert (nargs == 4);
 26991   return Fset_text_properties (args[0], args[1], args[2], args[3]);
 26992 }
 26993 
 26994 /* Contribute ELT to the mode line for window IT->w.  How it
 26995    translates into text depends on its data type.
 26996 
 26997    IT describes the display environment in which we display, as usual.
 26998 
 26999    DEPTH is the depth in recursion.  It is used to prevent
 27000    infinite recursion here.
 27001 
 27002    FIELD_WIDTH is the number of characters the display of ELT should
 27003    occupy in the mode line, and PRECISION is the maximum number of
 27004    characters to display from ELT's representation.  See
 27005    display_string for details.
 27006 
 27007    Returns the hpos of the end of the text generated by ELT.
 27008 
 27009    PROPS is a property list to add to any string we encounter.
 27010 
 27011    If RISKY, remove (disregard) any properties in any string
 27012    we encounter, and ignore :eval and :propertize.
 27013 
 27014    The global variable `mode_line_target' determines whether the
 27015    output is passed to `store_mode_line_noprop',
 27016    `store_mode_line_string', or `display_string'.  */
 27017 
 27018 static int
 27019 display_mode_element (struct it *it, int depth, int field_width, int precision,
 27020                       Lisp_Object elt, Lisp_Object props, bool risky)
 27021 {
 27022   int n = 0, field, prec;
 27023   bool literal = false;
 27024 
 27025  tail_recurse:
 27026   if (depth > 100)
 27027     elt = build_string ("*too-deep*");
 27028 
 27029   depth++;
 27030 
 27031   switch (XTYPE (elt))
 27032     {
 27033     case Lisp_String:
 27034       {
 27035         /* A string: output it and check for %-constructs within it.  */
 27036         unsigned char c;
 27037         ptrdiff_t offset = 0;
 27038 
 27039         if (SCHARS (elt) > 0
 27040             && (!NILP (props) || risky))
 27041           {
 27042             Lisp_Object oprops, aelt;
 27043             oprops = Ftext_properties_at (make_fixnum (0), elt);
 27044 
 27045             /* If the starting string's properties are not what
 27046                we want, translate the string.  Also, if the string
 27047                is risky, do that anyway.  */
 27048 
 27049             if (NILP (Fequal (props, oprops)) || risky)
 27050               {
 27051                 /* If the starting string has properties,
 27052                    merge the specified ones onto the existing ones.  */
 27053                 if (! NILP (oprops) && !risky)
 27054                   {
 27055                     Lisp_Object tem;
 27056 
 27057                     oprops = Fcopy_sequence (oprops);
 27058                     tem = props;
 27059                     while (CONSP (tem))
 27060                       {
 27061                         oprops = plist_put (oprops, XCAR (tem),
 27062                                             XCAR (XCDR (tem)));
 27063                         tem = XCDR (XCDR (tem));
 27064                       }
 27065                     props = oprops;
 27066                   }
 27067 
 27068                 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
 27069                 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
 27070                   {
 27071                     /* AELT is what we want.  Move it to the front
 27072                        without consing.  */
 27073                     elt = XCAR (aelt);
 27074                     mode_line_proptrans_alist
 27075                       = move_elt_to_front (aelt, mode_line_proptrans_alist);
 27076                   }
 27077                 else
 27078                   {
 27079                     Lisp_Object tem;
 27080 
 27081                     /* If AELT has the wrong props, it is useless.
 27082                        so get rid of it.  */
 27083                     if (! NILP (aelt))
 27084                       mode_line_proptrans_alist
 27085                         = Fdelq (aelt, mode_line_proptrans_alist);
 27086 
 27087                     elt = Fcopy_sequence (elt);
 27088                     /* PROPS might cause set-text-properties to signal
 27089                        an error, so we call it via internal_condition_case_n,
 27090                        to avoid an infloop in redisplay due to the error.  */
 27091                     internal_condition_case_n (safe_set_text_properties,
 27092                                                4,
 27093                                                ((Lisp_Object [])
 27094                                                {make_fixnum (0),
 27095                                                    Flength (elt),
 27096                                                    props,
 27097                                                    elt}),
 27098                                                Qt, safe_eval_handler);
 27099                     /* Add this item to mode_line_proptrans_alist.  */
 27100                     mode_line_proptrans_alist
 27101                       = Fcons (Fcons (elt, props),
 27102                                mode_line_proptrans_alist);
 27103                     /* Truncate mode_line_proptrans_alist
 27104                        to at most 50 elements.  */
 27105                     tem = Fnthcdr (make_fixnum (50),
 27106                                    mode_line_proptrans_alist);
 27107                     if (! NILP (tem))
 27108                       XSETCDR (tem, Qnil);
 27109                   }
 27110               }
 27111           }
 27112 
 27113         offset = 0;
 27114 
 27115         if (literal)
 27116           {
 27117             prec = precision - n;
 27118             switch (mode_line_target)
 27119               {
 27120               case MODE_LINE_NOPROP:
 27121               case MODE_LINE_TITLE:
 27122                 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
 27123                 break;
 27124               case MODE_LINE_STRING:
 27125                 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
 27126                 break;
 27127               case MODE_LINE_DISPLAY:
 27128                 n += display_string (NULL, elt, Qnil, 0, 0, it,
 27129                                      0, prec, 0, STRING_MULTIBYTE (elt));
 27130                 break;
 27131               }
 27132 
 27133             break;
 27134           }
 27135 
 27136         /* Handle the non-literal case.  */
 27137 
 27138         while ((precision <= 0 || n < precision)
 27139                && SREF (elt, offset) != 0
 27140                && (mode_line_target != MODE_LINE_DISPLAY
 27141                    || it->current_x < it->last_visible_x))
 27142           {
 27143             ptrdiff_t last_offset = offset;
 27144 
 27145             /* Advance to end of string or next format specifier.  */
 27146             while ((c = SREF (elt, offset++)) != '\0' && c != '%')
 27147               ;
 27148 
 27149             if (offset - 1 != last_offset)
 27150               {
 27151                 ptrdiff_t nchars, nbytes;
 27152 
 27153                 /* Output to end of string or up to '%'.  Field width
 27154                    is length of string.  Don't output more than
 27155                    PRECISION allows us.  */
 27156                 offset--;
 27157 
 27158                 prec = c_string_width (SDATA (elt) + last_offset,
 27159                                        offset - last_offset, precision - n,
 27160                                        &nchars, &nbytes);
 27161 
 27162                 switch (mode_line_target)
 27163                   {
 27164                   case MODE_LINE_NOPROP:
 27165                   case MODE_LINE_TITLE:
 27166                     n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
 27167                     break;
 27168                   case MODE_LINE_STRING:
 27169                     {
 27170                       ptrdiff_t bytepos = last_offset;
 27171                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27172                       ptrdiff_t endpos = (precision <= 0
 27173                                           ? string_byte_to_char (elt, offset)
 27174                                           : charpos + nchars);
 27175                       Lisp_Object mode_string
 27176                         = Fsubstring (elt, make_fixnum (charpos),
 27177                                       make_fixnum (endpos));
 27178                       n += store_mode_line_string (NULL, mode_string, false,
 27179                                                    0, 0, Qnil);
 27180                     }
 27181                     break;
 27182                   case MODE_LINE_DISPLAY:
 27183                     {
 27184                       ptrdiff_t bytepos = last_offset;
 27185                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27186 
 27187                       if (precision <= 0)
 27188                         nchars = string_byte_to_char (elt, offset) - charpos;
 27189                       n += display_string (NULL, elt, Qnil, 0, charpos,
 27190                                            it, 0, nchars, 0,
 27191                                            STRING_MULTIBYTE (elt));
 27192                     }
 27193                     break;
 27194                   }
 27195               }
 27196             else /* c == '%' */
 27197               {
 27198                 ptrdiff_t percent_position = offset;
 27199 
 27200                 /* Get the specified minimum width.  Zero means
 27201                    don't pad.  */
 27202                 field = 0;
 27203                 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
 27204                   field = field * 10 + c - '0';
 27205 
 27206                 /* Don't pad beyond the total padding allowed.  */
 27207                 if (field_width - n > 0 && field > field_width - n)
 27208                   field = field_width - n;
 27209 
 27210                 /* Note that either PRECISION <= 0 or N < PRECISION.  */
 27211                 prec = precision - n;
 27212 
 27213                 if (c == 'M')
 27214                   n += display_mode_element (it, depth, field, prec,
 27215                                              Vglobal_mode_string, props,
 27216                                              risky);
 27217                 else if (c != 0)
 27218                   {
 27219                     bool multibyte;
 27220                     ptrdiff_t bytepos, charpos;
 27221                     const char *spec;
 27222                     Lisp_Object string;
 27223 
 27224                     bytepos = percent_position;
 27225                     charpos = (STRING_MULTIBYTE (elt)
 27226                                ? string_byte_to_char (elt, bytepos)
 27227                                : bytepos);
 27228                     spec = decode_mode_spec (it->w, c, field, &string);
 27229                     eassert (NILP (string) || STRINGP (string));
 27230                     multibyte = !NILP (string) && STRING_MULTIBYTE (string);
 27231                     /* Non-ASCII characters in SPEC should cause mode-line
 27232                        element be displayed as a multibyte string.  */
 27233                     ptrdiff_t nbytes = strlen (spec);
 27234                     ptrdiff_t nchars, mb_nbytes;
 27235                     parse_str_as_multibyte ((const unsigned char *)spec, nbytes,
 27236                                             &nchars, &mb_nbytes);
 27237                     if (!(nbytes == nchars || nbytes != mb_nbytes))
 27238                       multibyte = true;
 27239 
 27240                     switch (mode_line_target)
 27241                       {
 27242                       case MODE_LINE_NOPROP:
 27243                       case MODE_LINE_TITLE:
 27244                         n += store_mode_line_noprop (spec, field, prec);
 27245                         break;
 27246                       case MODE_LINE_STRING:
 27247                         {
 27248                           Lisp_Object tem = build_string (spec);
 27249                           props = Ftext_properties_at (make_fixnum (charpos), elt);
 27250                           /* Should only keep face property in props */
 27251                           n += store_mode_line_string (NULL, tem, false,
 27252                                                        field, prec, props);
 27253                         }
 27254                         break;
 27255                       case MODE_LINE_DISPLAY:
 27256                         {
 27257                           int nglyphs_before, nwritten;
 27258 
 27259                           nglyphs_before = it->glyph_row->used[TEXT_AREA];
 27260                           nwritten = display_string (spec, string, elt,
 27261                                                      charpos, 0, it,
 27262                                                      field, prec, 0,
 27263                                                      multibyte);
 27264 
 27265                           /* Assign to the glyphs written above the
 27266                              string where the `%x' came from, position
 27267                              of the `%'.  */
 27268                           if (nwritten > 0)
 27269                             {
 27270                               struct glyph *glyph
 27271                                 = (it->glyph_row->glyphs[TEXT_AREA]
 27272                                    + nglyphs_before);
 27273                               int i;
 27274 
 27275                               for (i = 0; i < nwritten; ++i)
 27276                                 {
 27277                                   glyph[i].object = elt;
 27278                                   glyph[i].charpos = charpos;
 27279                                 }
 27280 
 27281                               n += nwritten;
 27282                             }
 27283                         }
 27284                         break;
 27285                       }
 27286                   }
 27287                 else /* c == 0 */
 27288                   break;
 27289               }
 27290           }
 27291       }
 27292       break;
 27293 
 27294     case Lisp_Symbol:
 27295       /* A symbol: process the value of the symbol recursively
 27296          as if it appeared here directly.  Avoid error if symbol void.
 27297          Special case: if value of symbol is a string, output the string
 27298          literally.  */
 27299       {
 27300         register Lisp_Object tem;
 27301 
 27302         /* If the variable is not marked as risky to set
 27303            then its contents are risky to use.  */
 27304         if (NILP (Fget (elt, Qrisky_local_variable)))
 27305           risky = true;
 27306 
 27307         tem = Fboundp (elt);
 27308         if (!NILP (tem))
 27309           {
 27310             tem = Fsymbol_value (elt);
 27311             /* If value is a string, output that string literally:
 27312                don't check for % within it.  */
 27313             if (STRINGP (tem))
 27314               literal = true;
 27315 
 27316             if (!EQ (tem, elt))
 27317               {
 27318                 /* Give up right away for nil or t.  */
 27319                 elt = tem;
 27320                 goto tail_recurse;
 27321               }
 27322           }
 27323       }
 27324       break;
 27325 
 27326     case Lisp_Cons:
 27327       {
 27328         register Lisp_Object car, tem;
 27329 
 27330         /* A cons cell: five distinct cases.
 27331            If first element is :eval or :propertize, do something special.
 27332            If first element is a string or a cons, process all the elements
 27333            and effectively concatenate them.
 27334            If first element is a negative number, truncate displaying cdr to
 27335            at most that many characters.  If positive, pad (with spaces)
 27336            to at least that many characters.
 27337            If first element is a symbol, process the cadr or caddr recursively
 27338            according to whether the symbol's value is non-nil or nil.  */
 27339         car = XCAR (elt);
 27340         if (EQ (car, QCeval))
 27341           {
 27342             /* An element of the form (:eval FORM) means evaluate FORM
 27343                and use the result as mode line elements.  */
 27344 
 27345             if (risky)
 27346               break;
 27347 
 27348             if (CONSP (XCDR (elt)))
 27349               {
 27350                 Lisp_Object spec;
 27351                 spec = safe__eval (true, XCAR (XCDR (elt)));
 27352                 /* The :eval form could delete the frame stored in the
 27353                    iterator, which will cause a crash if we try to
 27354                    access faces and other fields (e.g., FRAME_KBOARD)
 27355                    on that frame.  This is a nonsensical thing to do,
 27356                    and signaling an error from redisplay might be
 27357                    dangerous, but we cannot continue with an invalid frame.  */
 27358                 if (!FRAME_LIVE_P (it->f))
 27359                   signal_error (":eval deleted the frame being displayed", elt);
 27360                 n += display_mode_element (it, depth, field_width - n,
 27361                                            precision - n, spec, props,
 27362                                            risky);
 27363               }
 27364           }
 27365         else if (EQ (car, QCpropertize))
 27366           {
 27367             /* An element of the form (:propertize ELT PROPS...)
 27368                means display ELT but applying properties PROPS.  */
 27369 
 27370             if (risky)
 27371               break;
 27372 
 27373             if (CONSP (XCDR (elt)))
 27374               n += display_mode_element (it, depth, field_width - n,
 27375                                          precision - n, XCAR (XCDR (elt)),
 27376                                          XCDR (XCDR (elt)), risky);
 27377           }
 27378         else if (SYMBOLP (car))
 27379           {
 27380             tem = Fboundp (car);
 27381             elt = XCDR (elt);
 27382             if (!CONSP (elt))
 27383               goto invalid;
 27384             /* elt is now the cdr, and we know it is a cons cell.
 27385                Use its car if CAR has a non-nil value.  */
 27386             if (!NILP (tem))
 27387               {
 27388                 tem = Fsymbol_value (car);
 27389                 if (!NILP (tem))
 27390                   {
 27391                     elt = XCAR (elt);
 27392                     goto tail_recurse;
 27393                   }
 27394               }
 27395             /* Symbol's value is nil (or symbol is unbound)
 27396                Get the cddr of the original list
 27397                and if possible find the caddr and use that.  */
 27398             elt = XCDR (elt);
 27399             if (NILP (elt))
 27400               break;
 27401             else if (!CONSP (elt))
 27402               goto invalid;
 27403             elt = XCAR (elt);
 27404             goto tail_recurse;
 27405           }
 27406         else if (FIXNUMP (car))
 27407           {
 27408             register int lim = XFIXNUM (car);
 27409             elt = XCDR (elt);
 27410             if (lim < 0)
 27411               {
 27412                 /* Negative int means reduce maximum width.  */
 27413                 if (precision <= 0)
 27414                   precision = -lim;
 27415                 else
 27416                   precision = min (precision, -lim);
 27417               }
 27418             else if (lim > 0)
 27419               {
 27420                 /* Padding specified.  Don't let it be more than
 27421                    current maximum.  */
 27422                 if (precision > 0)
 27423                   lim = min (precision, lim);
 27424 
 27425                 /* If that's more padding than already wanted, queue it.
 27426                    But don't reduce padding already specified even if
 27427                    that is beyond the current truncation point.  */
 27428                 field_width = max (lim, field_width);
 27429               }
 27430             goto tail_recurse;
 27431           }
 27432         else if (STRINGP (car) || CONSP (car))
 27433           FOR_EACH_TAIL_SAFE (elt)
 27434             {
 27435               if (0 < precision && precision <= n)
 27436                 break;
 27437               n += display_mode_element (it, depth,
 27438                                          /* Pad after only the last
 27439                                             list element.  */
 27440                                          (! CONSP (XCDR (elt))
 27441                                           ? field_width - n
 27442                                           : 0),
 27443                                          precision - n, XCAR (elt),
 27444                                          props, risky);
 27445             }
 27446       }
 27447       break;
 27448 
 27449     default:
 27450     invalid:
 27451       elt = build_string ("*invalid*");
 27452       goto tail_recurse;
 27453     }
 27454 
 27455   /* Pad to FIELD_WIDTH.  */
 27456   if (field_width > 0 && n < field_width)
 27457     {
 27458       switch (mode_line_target)
 27459         {
 27460         case MODE_LINE_NOPROP:
 27461         case MODE_LINE_TITLE:
 27462           n += store_mode_line_noprop ("", field_width - n, 0);
 27463           break;
 27464         case MODE_LINE_STRING:
 27465           n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
 27466                                        Qnil);
 27467           break;
 27468         case MODE_LINE_DISPLAY:
 27469           n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
 27470                                0, 0, 0);
 27471           break;
 27472         }
 27473     }
 27474 
 27475   return n;
 27476 }
 27477 
 27478 /* Store a mode-line string element in mode_line_string_list.
 27479 
 27480    If STRING is non-null, display that C string.  Otherwise, the Lisp
 27481    string LISP_STRING is displayed.
 27482 
 27483    FIELD_WIDTH is the minimum number of output glyphs to produce.
 27484    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 27485    with spaces.  FIELD_WIDTH <= 0 means don't pad.
 27486 
 27487    PRECISION is the maximum number of characters to output from
 27488    STRING.  PRECISION <= 0  means don't truncate the string.
 27489 
 27490    If COPY_STRING, make a copy of LISP_STRING before adding
 27491    properties to the string.
 27492 
 27493    PROPS are the properties to add to the string.
 27494    The mode_line_string_face face property is always added to the string.
 27495  */
 27496 
 27497 static int
 27498 store_mode_line_string (const char *string, Lisp_Object lisp_string,
 27499                         bool copy_string,
 27500                         int field_width, int precision, Lisp_Object props)
 27501 {
 27502   ptrdiff_t len;
 27503   int n = 0;
 27504 
 27505   if (string != NULL)
 27506     {
 27507       len = strnlen (string, precision <= 0 ? SIZE_MAX : precision);
 27508       lisp_string = make_string (string, len);
 27509       if (NILP (props))
 27510         props = mode_line_string_face_prop;
 27511       else if (!NILP (mode_line_string_face))
 27512         {
 27513           Lisp_Object face = plist_get (props, Qface);
 27514           props = Fcopy_sequence (props);
 27515           if (NILP (face))
 27516             face = mode_line_string_face;
 27517           else
 27518             face = list2 (face, mode_line_string_face);
 27519           props = plist_put (props, Qface, face);
 27520         }
 27521       Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27522                             props, lisp_string);
 27523     }
 27524   else
 27525     {
 27526       len = SCHARS (lisp_string);
 27527       if (precision > 0 && len > precision)
 27528         {
 27529           len = precision;
 27530           lisp_string = Fsubstring (lisp_string, make_fixnum (0), make_fixnum (len));
 27531           precision = -1;
 27532         }
 27533       if (!NILP (mode_line_string_face))
 27534         {
 27535           Lisp_Object face;
 27536           if (NILP (props))
 27537             props = Ftext_properties_at (make_fixnum (0), lisp_string);
 27538           face = plist_get (props, Qface);
 27539           if (NILP (face))
 27540             face = mode_line_string_face;
 27541           else
 27542             face = list2 (face, mode_line_string_face);
 27543           props = list2 (Qface, face);
 27544           if (copy_string)
 27545             lisp_string = Fcopy_sequence (lisp_string);
 27546         }
 27547       if (!NILP (props))
 27548         Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27549                               props, lisp_string);
 27550     }
 27551 
 27552   if (len > 0)
 27553     {
 27554       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27555       n += len;
 27556     }
 27557 
 27558   if (field_width > len)
 27559     {
 27560       field_width -= len;
 27561       lisp_string = Fmake_string (make_fixnum (field_width), make_fixnum (' '),
 27562                                   Qnil);
 27563       if (!NILP (props))
 27564         Fadd_text_properties (make_fixnum (0), make_fixnum (field_width),
 27565                               props, lisp_string);
 27566       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27567       n += field_width;
 27568     }
 27569 
 27570   return n;
 27571 }
 27572 
 27573 
 27574 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
 27575        1, 4, 0,
 27576        doc: /* Format a string out of a mode line format specification.
 27577 First arg FORMAT specifies the mode line format (see `mode-line-format'
 27578 for details) to use.
 27579 
 27580 By default, the format is evaluated for the currently selected window.
 27581 
 27582 Optional second arg FACE specifies the face property to put on all
 27583 characters for which no face is specified.  The value nil means the
 27584 default face.  The value t means whatever face the window's mode line
 27585 currently uses (either `mode-line' or `mode-line-inactive',
 27586 depending on whether the window is the selected window or not).
 27587 An integer value means the value string has no text
 27588 properties.
 27589 
 27590 Optional third and fourth args WINDOW and BUFFER specify the window
 27591 and buffer to use as the context for the formatting (defaults
 27592 are the selected window and the WINDOW's buffer).  */)
 27593      (Lisp_Object format, Lisp_Object face,
 27594       Lisp_Object window, Lisp_Object buffer)
 27595 {
 27596   struct it it;
 27597   int len;
 27598   struct window *w;
 27599   struct buffer *old_buffer = NULL;
 27600   int face_id;
 27601   bool no_props = FIXNUMP (face);
 27602   specpdl_ref count = SPECPDL_INDEX ();
 27603   Lisp_Object str;
 27604   int string_start = 0;
 27605 
 27606   w = decode_any_window (window);
 27607   XSETWINDOW (window, w);
 27608 
 27609   if (NILP (buffer))
 27610     buffer = w->contents;
 27611   CHECK_BUFFER (buffer);
 27612 
 27613   /* Make formatting the modeline a non-op when noninteractive, otherwise
 27614      there will be problems later caused by a partially initialized frame.  */
 27615   if (NILP (format) || noninteractive)
 27616     return empty_unibyte_string;
 27617 
 27618   if (no_props)
 27619     face = Qnil;
 27620 
 27621   face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
 27622     : EQ (face, Qt) ? (EQ (window, selected_window)
 27623                        ? MODE_LINE_ACTIVE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
 27624     : EQ (face, Qmode_line_active) ? MODE_LINE_ACTIVE_FACE_ID
 27625     : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
 27626     : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
 27627     : EQ (face, Qtab_line) ? TAB_LINE_FACE_ID
 27628     : EQ (face, Qtab_bar) ? TAB_BAR_FACE_ID
 27629     : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
 27630     : DEFAULT_FACE_ID;
 27631 
 27632   old_buffer = current_buffer;
 27633 
 27634   /* Save things including mode_line_proptrans_alist,
 27635      and set that to nil so that we don't alter the outer value.  */
 27636   record_unwind_protect (unwind_format_mode_line,
 27637                          format_mode_line_unwind_data
 27638                            (XFRAME (WINDOW_FRAME (w)),
 27639                             old_buffer, selected_window, true));
 27640   mode_line_proptrans_alist = Qnil;
 27641 
 27642   Fselect_window (window, Qt);
 27643   set_buffer_internal_1 (XBUFFER (buffer));
 27644 
 27645   init_iterator (&it, w, -1, -1, NULL, face_id);
 27646 
 27647   if (no_props)
 27648     {
 27649       mode_line_target = MODE_LINE_NOPROP;
 27650       mode_line_string_face_prop = Qnil;
 27651       mode_line_string_list = Qnil;
 27652       string_start = MODE_LINE_NOPROP_LEN (0);
 27653     }
 27654   else
 27655     {
 27656       mode_line_target = MODE_LINE_STRING;
 27657       mode_line_string_list = Qnil;
 27658       mode_line_string_face = face;
 27659       mode_line_string_face_prop
 27660         = NILP (face) ? Qnil : list2 (Qface, face);
 27661     }
 27662 
 27663   push_kboard (FRAME_KBOARD (it.f));
 27664   display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27665   pop_kboard ();
 27666 
 27667   if (no_props)
 27668     {
 27669       len = MODE_LINE_NOPROP_LEN (string_start);
 27670       str = make_string (mode_line_noprop_buf + string_start, len);
 27671     }
 27672   else
 27673     {
 27674       mode_line_string_list = Fnreverse (mode_line_string_list);
 27675       str = Fmapconcat (Qidentity, mode_line_string_list,
 27676                         empty_unibyte_string);
 27677     }
 27678 
 27679   return unbind_to (count, str);
 27680 }
 27681 
 27682 /* Write a null-terminated, right justified decimal representation of
 27683    the positive integer D to BUF using a minimal field width WIDTH.  */
 27684 
 27685 static void
 27686 pint2str (register char *buf, register int width, register ptrdiff_t d)
 27687 {
 27688   register char *p = buf;
 27689 
 27690   if (d <= 0)
 27691     *p++ = '0';
 27692   else
 27693     {
 27694       while (d > 0)
 27695         {
 27696           *p++ = d % 10 + '0';
 27697           d /= 10;
 27698         }
 27699     }
 27700 
 27701   for (width -= (int) (p - buf); width > 0; --width)
 27702     *p++ = ' ';
 27703   *p-- = '\0';
 27704   while (p > buf)
 27705     {
 27706       d = *buf;
 27707       *buf++ = *p;
 27708       *p-- = d;
 27709     }
 27710 }
 27711 
 27712 /* Write a null-terminated, right justified decimal and "human
 27713    readable" representation of the nonnegative integer D to BUF using
 27714    a minimal field width WIDTH.  D should be smaller than 999.5e24. */
 27715 
 27716 static const char power_letter[] =
 27717   {
 27718     0,   /* no letter */
 27719     'k', /* kilo */
 27720     'M', /* mega */
 27721     'G', /* giga */
 27722     'T', /* tera */
 27723     'P', /* peta */
 27724     'E', /* exa */
 27725     'Z', /* zetta */
 27726     'Y'  /* yotta */
 27727   };
 27728 
 27729 static void
 27730 pint2hrstr (char *buf, int width, ptrdiff_t d)
 27731 {
 27732   /* We aim to represent the nonnegative integer D as
 27733      QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
 27734   ptrdiff_t quotient = d;
 27735   int remainder = 0;
 27736   /* -1 means: do not use TENTHS. */
 27737   int tenths = -1;
 27738   int exponent = 0;
 27739 
 27740   /* Length of QUOTIENT.TENTHS as a string. */
 27741   int length;
 27742 
 27743   char * psuffix;
 27744   char * p;
 27745 
 27746   if (quotient >= 1000)
 27747     {
 27748       /* Scale to the appropriate EXPONENT. */
 27749       do
 27750         {
 27751           remainder = quotient % 1000;
 27752           quotient /= 1000;
 27753           exponent++;
 27754         }
 27755       while (quotient >= 1000);
 27756 
 27757       /* Round to nearest and decide whether to use TENTHS or not. */
 27758       if (quotient <= 9)
 27759         {
 27760           tenths = remainder / 100;
 27761           if (remainder % 100 >= 50)
 27762             {
 27763               if (tenths < 9)
 27764                 tenths++;
 27765               else
 27766                 {
 27767                   quotient++;
 27768                   if (quotient == 10)
 27769                     tenths = -1;
 27770                   else
 27771                     tenths = 0;
 27772                 }
 27773             }
 27774         }
 27775       else
 27776         if (remainder >= 500)
 27777           {
 27778             if (quotient < 999)
 27779               quotient++;
 27780             else
 27781               {
 27782                 quotient = 1;
 27783                 exponent++;
 27784                 tenths = 0;
 27785               }
 27786           }
 27787     }
 27788 
 27789   /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
 27790   if (tenths == -1 && quotient <= 99)
 27791     if (quotient <= 9)
 27792       length = 1;
 27793     else
 27794       length = 2;
 27795   else
 27796     length = 3;
 27797   p = psuffix = buf + max (width, length);
 27798 
 27799   /* Print EXPONENT. */
 27800   *psuffix++ = power_letter[exponent];
 27801   *psuffix = '\0';
 27802 
 27803   /* Print TENTHS. */
 27804   if (tenths >= 0)
 27805     {
 27806       *--p = '0' + tenths;
 27807       *--p = '.';
 27808     }
 27809 
 27810   /* Print QUOTIENT. */
 27811   do
 27812     {
 27813       int digit = quotient % 10;
 27814       *--p =  '0' + digit;
 27815     }
 27816   while ((quotient /= 10) != 0);
 27817 
 27818   /* Print leading spaces. */
 27819   while (buf < p)
 27820     *--p = ' ';
 27821 }
 27822 
 27823 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
 27824    If EOL_FLAG, set also a mnemonic character for end-of-line
 27825    type of CODING_SYSTEM.  Return updated pointer into BUF.  */
 27826 
 27827 static unsigned char invalid_eol_type[] = "(*invalid*)";
 27828 
 27829 static char *
 27830 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
 27831 {
 27832   Lisp_Object val;
 27833   bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
 27834   const unsigned char *eol_str;
 27835   int eol_str_len;
 27836   /* The EOL conversion we are using.  */
 27837   Lisp_Object eoltype;
 27838 
 27839   val = CODING_SYSTEM_SPEC (coding_system);
 27840   eoltype = Qnil;
 27841 
 27842   if (!VECTORP (val))           /* Not yet decided.  */
 27843     {
 27844       *buf++ = multibyte ? '-' : ' ';
 27845       if (eol_flag)
 27846         eoltype = eol_mnemonic_undecided;
 27847       /* Don't mention EOL conversion if it isn't decided.  */
 27848     }
 27849   else
 27850     {
 27851       Lisp_Object attrs;
 27852       Lisp_Object eolvalue;
 27853 
 27854       attrs = AREF (val, 0);
 27855       eolvalue = AREF (val, 2);
 27856 
 27857       if (multibyte)
 27858         buf += CHAR_STRING (XFIXNAT (CODING_ATTR_MNEMONIC (attrs)),
 27859                             (unsigned char *) buf);
 27860       else
 27861         *buf++ = ' ';
 27862 
 27863       if (eol_flag)
 27864         {
 27865           /* The EOL conversion that is normal on this system.  */
 27866 
 27867           if (NILP (eolvalue))  /* Not yet decided.  */
 27868             eoltype = eol_mnemonic_undecided;
 27869           else if (VECTORP (eolvalue)) /* Not yet decided.  */
 27870             eoltype = eol_mnemonic_undecided;
 27871           else                  /* eolvalue is Qunix, Qdos, or Qmac.  */
 27872             eoltype = (EQ (eolvalue, Qunix)
 27873                        ? eol_mnemonic_unix
 27874                        : EQ (eolvalue, Qdos)
 27875                        ? eol_mnemonic_dos : eol_mnemonic_mac);
 27876         }
 27877     }
 27878 
 27879   if (eol_flag)
 27880     {
 27881       /* Mention the EOL conversion if it is not the usual one.  */
 27882       if (STRINGP (eoltype))
 27883         {
 27884           eol_str = SDATA (eoltype);
 27885           eol_str_len = SBYTES (eoltype);
 27886         }
 27887       else if (CHARACTERP (eoltype))
 27888         {
 27889           int c = XFIXNAT (eoltype);
 27890           return buf + CHAR_STRING (c, (unsigned char *) buf);
 27891         }
 27892       else
 27893         {
 27894           eol_str = invalid_eol_type;
 27895           eol_str_len = sizeof (invalid_eol_type) - 1;
 27896         }
 27897       memcpy (buf, eol_str, eol_str_len);
 27898       buf += eol_str_len;
 27899     }
 27900 
 27901   return buf;
 27902 }
 27903 
 27904 /* Return the approximate percentage N is of D (rounding upward), or 99,
 27905    whichever is less.  Assume 0 < D and 0 <= N <= D * INT_MAX / 100.  */
 27906 
 27907 static int
 27908 percent99 (ptrdiff_t n, ptrdiff_t d)
 27909 {
 27910   int percent = (d - 1 + 100.0 * n) / d;
 27911   return min (percent, 99);
 27912 }
 27913 
 27914 /* Return a string for the output of a mode line %-spec for window W,
 27915    generated by character C.  FIELD_WIDTH > 0 means pad the string
 27916    returned with spaces to that value.  Set *STRING to be a Lisp
 27917    string if the resulting string is taken from that Lisp string;
 27918    otherwise, set *STRING to Qnil.
 27919 
 27920    Note we operate on the current buffer for most purposes.  */
 27921 
 27922 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
 27923 
 27924 static const char *
 27925 decode_mode_spec (struct window *w, register int c, int field_width,
 27926                   Lisp_Object *string)
 27927 {
 27928   Lisp_Object obj;
 27929   struct frame *f = XFRAME (WINDOW_FRAME (w));
 27930   char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
 27931   /* We are going to use f->decode_mode_spec_buffer as the buffer to
 27932      produce strings from numerical values, so limit preposterously
 27933      large values of FIELD_WIDTH to avoid overrunning the buffer's
 27934      end.  The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
 27935      bytes plus the terminating null.  */
 27936   int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
 27937   struct buffer *b = current_buffer;
 27938 
 27939   obj = Qnil;
 27940   *string = Qnil;
 27941 
 27942   switch (c)
 27943     {
 27944     case '*':
 27945       if (!NILP (BVAR (b, read_only)))
 27946         return "%";
 27947       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 27948         return "*";
 27949       return "-";
 27950 
 27951     case '+':
 27952       /* This differs from %* only for a modified read-only buffer.  */
 27953       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 27954         return "*";
 27955       if (!NILP (BVAR (b, read_only)))
 27956         return "%";
 27957       return "-";
 27958 
 27959     case '&':
 27960       /* This differs from %* in ignoring read-only-ness.  */
 27961       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 27962         return "*";
 27963       return "-";
 27964 
 27965     case '%':
 27966       return "%";
 27967 
 27968     case '[':
 27969       {
 27970         int i;
 27971         char *p;
 27972 
 27973         if (command_loop_level > 5)
 27974           return "[[[... ";
 27975         p = decode_mode_spec_buf;
 27976         for (i = 0; i < command_loop_level; i++)
 27977           *p++ = '[';
 27978         *p = 0;
 27979         return decode_mode_spec_buf;
 27980       }
 27981 
 27982     case ']':
 27983       {
 27984         int i;
 27985         char *p;
 27986 
 27987         if (command_loop_level > 5)
 27988           return " ...]]]";
 27989         p = decode_mode_spec_buf;
 27990         for (i = 0; i < command_loop_level; i++)
 27991           *p++ = ']';
 27992         *p = 0;
 27993         return decode_mode_spec_buf;
 27994       }
 27995 
 27996     case '-':
 27997       {
 27998         register int i;
 27999 
 28000         /* Let lots_of_dashes be a string of infinite length.  */
 28001         if (mode_line_target == MODE_LINE_NOPROP
 28002             || mode_line_target == MODE_LINE_STRING)
 28003           return "--";
 28004         if (field_width <= 0
 28005             || field_width > sizeof (lots_of_dashes))
 28006           {
 28007             for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
 28008               decode_mode_spec_buf[i] = '-';
 28009             decode_mode_spec_buf[i] = '\0';
 28010             return decode_mode_spec_buf;
 28011           }
 28012         else
 28013           return lots_of_dashes;
 28014       }
 28015 
 28016     case 'b':
 28017       obj = BVAR (b, name);
 28018       break;
 28019 
 28020     case 'c':
 28021     case 'C':
 28022       /* %c, %C, and %l are ignored in `frame-title-format'.
 28023          (In redisplay_internal, the frame title is drawn _before_ the
 28024          windows are updated, so the stuff which depends on actual
 28025          window contents (such as %l) may fail to render properly, or
 28026          even crash emacs.)  */
 28027       if (mode_line_target == MODE_LINE_TITLE)
 28028         return "";
 28029       else
 28030         {
 28031           ptrdiff_t col = current_column ();
 28032           int disp_col = (c == 'C') ? col + 1 : col;
 28033           w->column_number_displayed = col;
 28034           pint2str (decode_mode_spec_buf, width, disp_col);
 28035           return decode_mode_spec_buf;
 28036         }
 28037 
 28038     case 'e':
 28039 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
 28040       {
 28041         if (NILP (Vmemory_full))
 28042           return "";
 28043         else
 28044           return "!MEM FULL! ";
 28045       }
 28046 #else
 28047       return "";
 28048 #endif
 28049 
 28050     case 'F':
 28051       /* %F displays the frame name.  */
 28052       if (!NILP (f->title))
 28053         return SSDATA (f->title);
 28054       if (f->explicit_name || ! FRAME_WINDOW_P (f))
 28055         return SSDATA (f->name);
 28056       return "Emacs";
 28057 
 28058     case 'f':
 28059       obj = BVAR (b, filename);
 28060       break;
 28061 
 28062     case 'i':
 28063       {
 28064         ptrdiff_t size = ZV - BEGV;
 28065         pint2str (decode_mode_spec_buf, width, size);
 28066         return decode_mode_spec_buf;
 28067       }
 28068 
 28069     case 'I':
 28070       {
 28071         ptrdiff_t size = ZV - BEGV;
 28072         pint2hrstr (decode_mode_spec_buf, width, size);
 28073         return decode_mode_spec_buf;
 28074       }
 28075 
 28076     case 'l':
 28077       {
 28078         ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
 28079         ptrdiff_t topline, nlines, height;
 28080         ptrdiff_t junk;
 28081 
 28082         /* %c, %C, and %l are ignored in `frame-title-format'.  */
 28083         if (mode_line_target == MODE_LINE_TITLE)
 28084           return "";
 28085 
 28086         startpos = marker_position (w->start);
 28087         startpos_byte = marker_byte_position (w->start);
 28088         height = WINDOW_TOTAL_LINES (w);
 28089         /* We cannot cope with w->start being outside of the
 28090            accessible portion of the buffer; in particular,
 28091            display_count_lines call below might infloop if called with
 28092            startpos_byte outside of the [BEGV_BYTE..ZV_BYTE] region.
 28093            Such w->start means we were called in some "creative" way
 28094            when the buffer's restriction was changed, but the window
 28095            wasn't yet redisplayed after that.  If that happens, we
 28096            need to determine a new base line.  */
 28097         if (!(BUF_BEGV_BYTE (b) <= startpos_byte
 28098               && startpos_byte <= BUF_ZV_BYTE (b)))
 28099           {
 28100             startpos = BUF_BEGV (b);
 28101             startpos_byte = BUF_BEGV_BYTE (b);
 28102             w->base_line_pos = 0;
 28103             w->base_line_number = 0;
 28104           }
 28105 
 28106         /* If we decided that this buffer isn't suitable for line numbers,
 28107            don't forget that too fast.  */
 28108         if (w->base_line_pos == -1)
 28109           goto no_value;
 28110 
 28111         /* If the buffer is very big, don't waste time.  */
 28112         if (FIXNUMP (Vline_number_display_limit)
 28113             && BUF_ZV (b) - BUF_BEGV (b) > XFIXNUM (Vline_number_display_limit))
 28114           {
 28115             w->base_line_pos = 0;
 28116             w->base_line_number = 0;
 28117             goto no_value;
 28118           }
 28119 
 28120         if (w->base_line_number > 0
 28121             && w->base_line_pos > 0
 28122             && w->base_line_pos <= startpos)
 28123           {
 28124             line = w->base_line_number;
 28125             linepos = w->base_line_pos;
 28126             linepos_byte = buf_charpos_to_bytepos (b, linepos);
 28127           }
 28128         else
 28129           {
 28130             line = 1;
 28131             linepos = BUF_BEGV (b);
 28132             linepos_byte = BUF_BEGV_BYTE (b);
 28133           }
 28134 
 28135         /* Count lines from base line to window start position.  */
 28136         nlines = display_count_lines (linepos_byte,
 28137                                       startpos_byte,
 28138                                       startpos, &junk);
 28139 
 28140         topline = nlines + line;
 28141 
 28142         /* Determine a new base line, if the old one is too close
 28143            or too far away, or if we did not have one.
 28144            "Too close" means it's plausible a scroll-down would
 28145            go back past it.  */
 28146         if (startpos == BUF_BEGV (b))
 28147           {
 28148             w->base_line_number = topline;
 28149             w->base_line_pos = BUF_BEGV (b);
 28150           }
 28151         else if (nlines < height + 25 || nlines > height * 3 + 50
 28152                  || linepos == BUF_BEGV (b))
 28153           {
 28154             ptrdiff_t limit = BUF_BEGV (b);
 28155             ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
 28156             ptrdiff_t position;
 28157             ptrdiff_t distance
 28158               = (line_number_display_limit_width < 0 ? 0
 28159                  : INT_MULTIPLY_WRAPV (line_number_display_limit_width,
 28160                                        height * 2 + 30,
 28161                                        &distance)
 28162                  ? PTRDIFF_MAX : distance);
 28163 
 28164             if (startpos - distance > limit)
 28165               {
 28166                 limit = startpos - distance;
 28167                 limit_byte = CHAR_TO_BYTE (limit);
 28168               }
 28169 
 28170             nlines = display_count_lines (startpos_byte,
 28171                                           limit_byte,
 28172                                           - (height * 2 + 30),
 28173                                           &position);
 28174             /* If we couldn't find the lines we wanted within
 28175                line_number_display_limit_width chars per line,
 28176                give up on line numbers for this window.  */
 28177             if (position == limit_byte && limit == startpos - distance)
 28178               {
 28179                 w->base_line_pos = -1;
 28180                 w->base_line_number = 0;
 28181                 goto no_value;
 28182               }
 28183 
 28184             w->base_line_number = topline - nlines;
 28185             w->base_line_pos = BYTE_TO_CHAR (position);
 28186           }
 28187 
 28188         /* Now count lines from the start pos to point.  */
 28189         nlines = display_count_lines (startpos_byte,
 28190                                       PT_BYTE, PT, &junk);
 28191 
 28192         /* Record that we did display the line number.  */
 28193         line_number_displayed = true;
 28194 
 28195         /* Make the string to show.  */
 28196         pint2str (decode_mode_spec_buf, width, topline + nlines);
 28197         return decode_mode_spec_buf;
 28198     no_value:
 28199         {
 28200           char *p = decode_mode_spec_buf;
 28201           int pad = width - 2;
 28202           while (pad-- > 0)
 28203             *p++ = ' ';
 28204           *p++ = '?';
 28205           *p++ = '?';
 28206           *p = '\0';
 28207           return decode_mode_spec_buf;
 28208         }
 28209       }
 28210       break;
 28211 
 28212     case 'm':
 28213       obj = BVAR (b, mode_name);
 28214       break;
 28215 
 28216     case 'n':
 28217       if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
 28218         return " Narrow";
 28219       break;
 28220 
 28221       /* Display the "degree of travel" of the window through the buffer.  */
 28222     case 'o':
 28223       {
 28224         ptrdiff_t toppos = marker_position (w->start);
 28225         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28226         ptrdiff_t begv = BUF_BEGV (b);
 28227         ptrdiff_t zv = BUF_ZV (b);
 28228 
 28229         if (zv <= botpos)
 28230           return toppos <= begv ? "All" : "Bottom";
 28231         else if (toppos <= begv)
 28232           return "Top";
 28233         else
 28234           {
 28235           sprintf (decode_mode_spec_buf, "%2d%%",
 28236                    percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
 28237           return decode_mode_spec_buf;
 28238           }
 28239       }
 28240 
 28241       /* Display percentage of buffer above the top of the screen.  */
 28242     case 'p':
 28243       {
 28244         ptrdiff_t pos = marker_position (w->start);
 28245         ptrdiff_t begv = BUF_BEGV (b);
 28246         ptrdiff_t zv = BUF_ZV (b);
 28247 
 28248         if (w->window_end_pos <= BUF_Z (b) - zv)
 28249           return pos <= begv ? "All" : "Bottom";
 28250         else if (pos <= begv)
 28251           return "Top";
 28252         else
 28253           {
 28254             sprintf (decode_mode_spec_buf, "%2d%%",
 28255                      percent99 (pos - begv, zv - begv));
 28256             return decode_mode_spec_buf;
 28257           }
 28258       }
 28259 
 28260       /* Display percentage of size above the bottom of the screen.  */
 28261     case 'P':
 28262       {
 28263         ptrdiff_t toppos = marker_position (w->start);
 28264         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28265         ptrdiff_t begv = BUF_BEGV (b);
 28266         ptrdiff_t zv = BUF_ZV (b);
 28267 
 28268         if (zv <= botpos)
 28269           return toppos <= begv ? "All" : "Bottom";
 28270         else
 28271           {
 28272             sprintf (decode_mode_spec_buf,
 28273                      &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
 28274                      percent99 (botpos - begv, zv - begv));
 28275             return decode_mode_spec_buf;
 28276           }
 28277       }
 28278 
 28279       /* Display percentage offsets of top and bottom of the window,
 28280          using "All" (but not "Top" or "Bottom") where appropriate.  */
 28281     case 'q':
 28282       {
 28283         ptrdiff_t toppos = marker_position (w->start);
 28284         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28285         ptrdiff_t begv = BUF_BEGV (b);
 28286         ptrdiff_t zv = BUF_ZV (b);
 28287         int top_perc, bot_perc;
 28288 
 28289         if ((toppos <= begv) && (zv <= botpos))
 28290           return "All   ";
 28291 
 28292         top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
 28293         bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
 28294 
 28295         if (top_perc == bot_perc)
 28296           sprintf (decode_mode_spec_buf, "%d%%", top_perc);
 28297         else
 28298           sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
 28299 
 28300         return decode_mode_spec_buf;
 28301       }
 28302 
 28303     case 's':
 28304       /* status of process */
 28305       obj = Fget_buffer_process (Fcurrent_buffer ());
 28306       if (NILP (obj))
 28307         return "no process";
 28308 #ifndef MSDOS
 28309       obj = Fsymbol_name (Fprocess_status (obj));
 28310 #endif
 28311       break;
 28312 
 28313     case '@':
 28314       {
 28315         specpdl_ref count = inhibit_garbage_collection ();
 28316         Lisp_Object curdir = BVAR (current_buffer, directory);
 28317         Lisp_Object val = Qnil;
 28318 
 28319         if (STRINGP (curdir))
 28320           val = safe_call1 (intern ("file-remote-p"), curdir);
 28321 
 28322         val = unbind_to (count, val);
 28323 
 28324         if (NILP (val))
 28325           return "-";
 28326         else
 28327           return "@";
 28328       }
 28329 
 28330     case 'z':
 28331       /* coding-system (not including end-of-line format) */
 28332     case 'Z':
 28333       /* coding-system (including end-of-line type) */
 28334       {
 28335         bool eol_flag = (c == 'Z');
 28336         char *p = decode_mode_spec_buf;
 28337 
 28338         if (! FRAME_WINDOW_P (f))
 28339           {
 28340             /* No need to mention EOL here--the terminal never needs
 28341                to do EOL conversion.  */
 28342             p = decode_mode_spec_coding (CODING_ID_NAME
 28343                                          (FRAME_KEYBOARD_CODING (f)->id),
 28344                                          p, false);
 28345             p = decode_mode_spec_coding (CODING_ID_NAME
 28346                                          (FRAME_TERMINAL_CODING (f)->id),
 28347                                          p, false);
 28348           }
 28349         p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
 28350                                      p, eol_flag);
 28351 
 28352 #if false /* This proves to be annoying; I think we can do without. -- rms.  */
 28353 #ifdef subprocesses
 28354         obj = Fget_buffer_process (Fcurrent_buffer ());
 28355         if (PROCESSP (obj))
 28356           {
 28357             p = decode_mode_spec_coding
 28358               (XPROCESS (obj)->decode_coding_system, p, eol_flag);
 28359             p = decode_mode_spec_coding
 28360               (XPROCESS (obj)->encode_coding_system, p, eol_flag);
 28361           }
 28362 #endif /* subprocesses */
 28363 #endif /* false */
 28364         *p = 0;
 28365         return decode_mode_spec_buf;
 28366       }
 28367     }
 28368 
 28369   if (STRINGP (obj))
 28370     {
 28371       *string = obj;
 28372       return SSDATA (obj);
 28373     }
 28374   else
 28375     return "";
 28376 }
 28377 
 28378 /* Return the number of lines between start_byte and end_byte in the
 28379    current buffer. */
 28380 
 28381 ptrdiff_t
 28382 count_lines (ptrdiff_t start_byte, ptrdiff_t end_byte)
 28383 {
 28384   ptrdiff_t ignored;
 28385   return display_count_lines (start_byte, end_byte, ZV, &ignored);
 28386 }
 28387 
 28388 /* Count up to COUNT lines starting from START_BYTE.  COUNT negative
 28389    means count lines back from START_BYTE.  But don't go beyond
 28390    LIMIT_BYTE.  Return the number of lines thus found (always
 28391    nonnegative).
 28392 
 28393    Set *BYTE_POS_PTR to the byte position where we stopped.  This is
 28394    either the position COUNT lines after/before START_BYTE, if we
 28395    found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
 28396    COUNT lines.  */
 28397 
 28398 static ptrdiff_t
 28399 display_count_lines (ptrdiff_t start_byte,
 28400                      ptrdiff_t limit_byte, ptrdiff_t count,
 28401                      ptrdiff_t *byte_pos_ptr)
 28402 {
 28403   register unsigned char *cursor;
 28404   unsigned char *base;
 28405 
 28406   register ptrdiff_t ceiling;
 28407   register unsigned char *ceiling_addr;
 28408   ptrdiff_t orig_count = count;
 28409 
 28410   /* If we are not in selective display mode,
 28411      check only for newlines.  */
 28412   bool selective_display
 28413     = (!NILP (BVAR (current_buffer, selective_display))
 28414        && !FIXNUMP (BVAR (current_buffer, selective_display)));
 28415 
 28416   if (count > 0)
 28417     {
 28418       while (start_byte < limit_byte)
 28419         {
 28420           ceiling =  BUFFER_CEILING_OF (start_byte);
 28421           ceiling = min (limit_byte - 1, ceiling);
 28422           ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
 28423           base = (cursor = BYTE_POS_ADDR (start_byte));
 28424 
 28425           do
 28426             {
 28427               if (selective_display)
 28428                 {
 28429                   while (*cursor != '\n' && *cursor != 015
 28430                          && ++cursor != ceiling_addr)
 28431                     continue;
 28432                   if (cursor == ceiling_addr)
 28433                     break;
 28434                 }
 28435               else
 28436                 {
 28437                   cursor = memchr (cursor, '\n', ceiling_addr - cursor);
 28438                   if (! cursor)
 28439                     break;
 28440                 }
 28441 
 28442               cursor++;
 28443 
 28444               if (--count == 0)
 28445                 {
 28446                   start_byte += cursor - base;
 28447                   *byte_pos_ptr = start_byte;
 28448                   return orig_count;
 28449                 }
 28450             }
 28451           while (cursor < ceiling_addr);
 28452 
 28453           start_byte += ceiling_addr - base;
 28454         }
 28455     }
 28456   else
 28457     {
 28458       while (start_byte > limit_byte)
 28459         {
 28460           ceiling = BUFFER_FLOOR_OF (start_byte - 1);
 28461           ceiling = max (limit_byte, ceiling);
 28462           ceiling_addr = BYTE_POS_ADDR (ceiling);
 28463           base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
 28464           while (true)
 28465             {
 28466               if (selective_display)
 28467                 {
 28468                   while (--cursor >= ceiling_addr
 28469                          && *cursor != '\n' && *cursor != 015)
 28470                     continue;
 28471                   if (cursor < ceiling_addr)
 28472                     break;
 28473                 }
 28474               else
 28475                 {
 28476                   cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
 28477                   if (! cursor)
 28478                     break;
 28479                 }
 28480 
 28481               if (++count == 0)
 28482                 {
 28483                   start_byte += cursor - base + 1;
 28484                   *byte_pos_ptr = start_byte;
 28485                   /* When scanning backwards, we should
 28486                      not count the newline posterior to which we stop.  */
 28487                   return - orig_count - 1;
 28488                 }
 28489             }
 28490           start_byte += ceiling_addr - base;
 28491         }
 28492     }
 28493 
 28494   *byte_pos_ptr = limit_byte;
 28495 
 28496   if (count < 0)
 28497     return - orig_count + count;
 28498   return orig_count - count;
 28499 
 28500 }
 28501 
 28502 
 28503 
 28504 /***********************************************************************
 28505                          Displaying strings
 28506  ***********************************************************************/
 28507 
 28508 /* Display a NUL-terminated string, starting with index START.
 28509 
 28510    If STRING is non-null, display that C string.  Otherwise, the Lisp
 28511    string LISP_STRING is displayed.  There's a case that STRING is
 28512    non-null and LISP_STRING is not nil.  It means STRING is a string
 28513    data of LISP_STRING.  In that case, we display LISP_STRING while
 28514    ignoring its text properties.
 28515 
 28516    If FACE_STRING is not nil, FACE_STRING_POS is a position in
 28517    FACE_STRING.  Display STRING or LISP_STRING with the face at
 28518    FACE_STRING_POS in FACE_STRING:
 28519 
 28520    Display the string in the environment given by IT, but use the
 28521    standard display table, temporarily.
 28522 
 28523    FIELD_WIDTH is the minimum number of output glyphs to produce.
 28524    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 28525    with spaces.  If STRING has more characters, more than FIELD_WIDTH
 28526    glyphs will be produced.  FIELD_WIDTH <= 0 means don't pad.
 28527 
 28528    PRECISION is the maximum number of characters to output from
 28529    STRING.  PRECISION < 0  means don't truncate the string.
 28530 
 28531    This is roughly equivalent to printf format specifiers:
 28532 
 28533    FIELD_WIDTH  PRECISION       PRINTF
 28534    ----------------------------------------
 28535    -1           -1              %s
 28536    -1           10              %.10s
 28537    10           -1              %10s
 28538    20           10              %20.10s
 28539 
 28540    MULTIBYTE zero means do not display multibyte chars, > 0 means do
 28541    display them, and < 0 means obey the current buffer's value of
 28542    enable_multibyte_characters.
 28543 
 28544    Value is the number of columns displayed.  */
 28545 
 28546 static int
 28547 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
 28548                 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
 28549                 int field_width, int precision, int max_x, int multibyte)
 28550 {
 28551   int hpos_at_start = it->hpos;
 28552   int saved_face_id = it->face_id;
 28553   struct glyph_row *row = it->glyph_row;
 28554   ptrdiff_t it_charpos;
 28555 
 28556   /* Initialize the iterator IT for iteration over STRING beginning
 28557      with index START.  */
 28558   reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string,
 28559                     start, precision, field_width, multibyte);
 28560 
 28561   if (string && STRINGP (lisp_string))
 28562     /* LISP_STRING is the one returned by decode_mode_spec.  We should
 28563        ignore its text properties.  */
 28564     it->stop_charpos = it->end_charpos;
 28565 
 28566   /* If displaying STRING, set up the face of the iterator from
 28567      FACE_STRING, if that's given.  */
 28568   if (STRINGP (face_string))
 28569     {
 28570       ptrdiff_t endptr;
 28571       struct face *face;
 28572 
 28573       it->face_id
 28574         = face_at_string_position (it->w, face_string, face_string_pos,
 28575                                    0, &endptr, it->base_face_id, false, 0);
 28576       face = FACE_FROM_ID (it->f, it->face_id);
 28577       it->face_box_p = face->box != FACE_NO_BOX;
 28578 
 28579       /* If we have a display spec, but there's no Lisp string being
 28580          displayed, then check whether we've got one from the
 28581          :propertize being passed in and use that.  */
 28582       if (NILP (lisp_string))
 28583         {
 28584           Lisp_Object display = Fget_text_property (make_fixnum (0), Qdisplay,
 28585                                                     face_string);
 28586           if (!NILP (display))
 28587             {
 28588               Lisp_Object min_width = plist_get (display, Qmin_width);
 28589               if (!NILP (min_width))
 28590                 display_min_width (it, 0, face_string, min_width);
 28591             }
 28592         }
 28593     }
 28594 
 28595   /* Set max_x to the maximum allowed X position.  Don't let it go
 28596      beyond the right edge of the window.  */
 28597   if (max_x <= 0)
 28598     max_x = it->last_visible_x;
 28599   else
 28600     max_x = min (max_x, it->last_visible_x);
 28601 
 28602   /* Skip over display elements that are not visible because IT->w is
 28603      hscrolled.  */
 28604   if (it->current_x < it->first_visible_x)
 28605     move_it_in_display_line_to (it, 100000, it->first_visible_x,
 28606                                 MOVE_TO_POS | MOVE_TO_X);
 28607 
 28608   row->ascent = it->max_ascent;
 28609   row->height = it->max_ascent + it->max_descent;
 28610   row->phys_ascent = it->max_phys_ascent;
 28611   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 28612   row->extra_line_spacing = it->max_extra_line_spacing;
 28613 
 28614   if (STRINGP (it->string))
 28615     it_charpos = IT_STRING_CHARPOS (*it);
 28616   else
 28617     it_charpos = IT_CHARPOS (*it);
 28618 
 28619   /* This condition is for the case that we are called with current_x
 28620      past last_visible_x.  */
 28621   while (it->current_x < max_x)
 28622     {
 28623       int x_before, x, n_glyphs_before, i, nglyphs;
 28624 
 28625       /* Get the next display element.  */
 28626       if (!get_next_display_element (it))
 28627         break;
 28628 
 28629       /* Produce glyphs.  */
 28630       x_before = it->current_x;
 28631       n_glyphs_before = row->used[TEXT_AREA];
 28632       PRODUCE_GLYPHS (it);
 28633 
 28634       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 28635       i = 0;
 28636       x = x_before;
 28637       while (i < nglyphs)
 28638         {
 28639           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 28640 
 28641           if (it->line_wrap != TRUNCATE
 28642               && x + glyph->pixel_width > max_x)
 28643             {
 28644               /* End of continued line or max_x reached.  */
 28645               if (CHAR_GLYPH_PADDING_P (*glyph))
 28646                 {
 28647                   /* A wide character is unbreakable.  */
 28648                   if (row->reversed_p)
 28649                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28650                                       - n_glyphs_before);
 28651                   row->used[TEXT_AREA] = n_glyphs_before;
 28652                   it->current_x = x_before;
 28653                 }
 28654               else
 28655                 {
 28656                   if (row->reversed_p)
 28657                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28658                                       - (n_glyphs_before + i));
 28659                   row->used[TEXT_AREA] = n_glyphs_before + i;
 28660                   it->current_x = x;
 28661                 }
 28662               break;
 28663             }
 28664           else if (x + glyph->pixel_width >= it->first_visible_x)
 28665             {
 28666               /* Glyph is at least partially visible.  */
 28667               ++it->hpos;
 28668               if (x < it->first_visible_x)
 28669                 row->x = x - it->first_visible_x;
 28670             }
 28671           else
 28672             {
 28673               /* Glyph is off the left margin of the display area.
 28674                  Should not happen.  */
 28675               emacs_abort ();
 28676             }
 28677 
 28678           row->ascent = max (row->ascent, it->max_ascent);
 28679           row->height = max (row->height, it->max_ascent + it->max_descent);
 28680           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 28681           row->phys_height = max (row->phys_height,
 28682                                   it->max_phys_ascent + it->max_phys_descent);
 28683           row->extra_line_spacing = max (row->extra_line_spacing,
 28684                                          it->max_extra_line_spacing);
 28685           x += glyph->pixel_width;
 28686           ++i;
 28687         }
 28688 
 28689       /* Stop if max_x reached.  */
 28690       if (i < nglyphs)
 28691         break;
 28692 
 28693       /* Stop at line ends.  */
 28694       if (ITERATOR_AT_END_OF_LINE_P (it))
 28695         {
 28696           it->continuation_lines_width = 0;
 28697           break;
 28698         }
 28699 
 28700       set_iterator_to_next (it, true);
 28701       if (STRINGP (it->string))
 28702         it_charpos = IT_STRING_CHARPOS (*it);
 28703       else
 28704         it_charpos = IT_CHARPOS (*it);
 28705 
 28706       /* Stop if truncating at the right edge.  */
 28707       if (it->line_wrap == TRUNCATE
 28708           && it->current_x >= it->last_visible_x)
 28709         {
 28710           /* Add truncation mark, but don't do it if the line is
 28711              truncated at a padding space.  */
 28712           if (it_charpos < it->string_nchars)
 28713             {
 28714               if (!FRAME_WINDOW_P (it->f))
 28715                 {
 28716                   int ii, n;
 28717 
 28718                   if (it->current_x > it->last_visible_x)
 28719                     {
 28720                       if (!row->reversed_p)
 28721                         {
 28722                           for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
 28723                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 28724                               break;
 28725                         }
 28726                       else
 28727                         {
 28728                           for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
 28729                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 28730                               break;
 28731                           unproduce_glyphs (it, ii + 1);
 28732                           ii = row->used[TEXT_AREA] - (ii + 1);
 28733                         }
 28734                       for (n = row->used[TEXT_AREA]; ii < n; ++ii)
 28735                         {
 28736                           row->used[TEXT_AREA] = ii;
 28737                           produce_special_glyphs (it, IT_TRUNCATION);
 28738                         }
 28739                     }
 28740                   produce_special_glyphs (it, IT_TRUNCATION);
 28741                 }
 28742               row->truncated_on_right_p = true;
 28743             }
 28744           break;
 28745         }
 28746     }
 28747 
 28748   /* Maybe insert a truncation at the left.  */
 28749   if (it->first_visible_x
 28750       && it_charpos > 0)
 28751     {
 28752       if (!FRAME_WINDOW_P (it->f)
 28753           || (row->reversed_p
 28754               ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 28755               : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 28756         insert_left_trunc_glyphs (it);
 28757       row->truncated_on_left_p = true;
 28758     }
 28759 
 28760   it->face_id = saved_face_id;
 28761 
 28762   /* Value is number of columns displayed.  */
 28763   return it->hpos - hpos_at_start;
 28764 }
 28765 
 28766 
 28767 
 28768 /* This is like a combination of memq and assq.  Return 1/2 if PROPVAL
 28769    appears as an element of LIST or as the car of an element of LIST.
 28770    If PROPVAL is a list, compare each element against LIST in that
 28771    way, and return 1/2 if any element of PROPVAL is found in LIST.
 28772    Otherwise return 0.  This function cannot quit.
 28773    The return value is 2 if the text is invisible but with an ellipsis
 28774    and 1 if it's invisible and without an ellipsis.  */
 28775 
 28776 int
 28777 invisible_prop (Lisp_Object propval, Lisp_Object list)
 28778 {
 28779   Lisp_Object tail, proptail;
 28780 
 28781   for (tail = list; CONSP (tail); tail = XCDR (tail))
 28782     {
 28783       register Lisp_Object tem;
 28784       tem = XCAR (tail);
 28785       if (EQ (propval, tem))
 28786         return 1;
 28787       if (CONSP (tem) && EQ (propval, XCAR (tem)))
 28788         return NILP (XCDR (tem)) ? 1 : 2;
 28789     }
 28790 
 28791   if (CONSP (propval))
 28792     {
 28793       for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
 28794         {
 28795           Lisp_Object propelt;
 28796           propelt = XCAR (proptail);
 28797           for (tail = list; CONSP (tail); tail = XCDR (tail))
 28798             {
 28799               register Lisp_Object tem;
 28800               tem = XCAR (tail);
 28801               if (EQ (propelt, tem))
 28802                 return 1;
 28803               if (CONSP (tem) && EQ (propelt, XCAR (tem)))
 28804                 return NILP (XCDR (tem)) ? 1 : 2;
 28805             }
 28806         }
 28807     }
 28808 
 28809   return 0;
 28810 }
 28811 
 28812 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
 28813        doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
 28814 POS should be a marker or a buffer position; the value of the `invisible'
 28815 property at that position in the current buffer is examined.
 28816 POS can also be the actual value of the `invisible' text or overlay
 28817 property of the text of interest, in which case the value itself is
 28818 examined.
 28819 
 28820 The non-nil value returned can be t for currently invisible text that is
 28821 entirely hidden on display, or some other non-nil, non-t value if the
 28822 text is replaced by an ellipsis.
 28823 
 28824 Note that whether text with `invisible' property is actually hidden on
 28825 display may depend on `buffer-invisibility-spec', which see.  */)
 28826   (Lisp_Object pos)
 28827 {
 28828   Lisp_Object prop
 28829     = (FIXNATP (pos) || MARKERP (pos)
 28830        ? Fget_char_property (pos, Qinvisible, Qnil)
 28831        : pos);
 28832   int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
 28833   return (invis == 0 ? Qnil
 28834           : invis == 1 ? Qt
 28835           : make_fixnum (invis));
 28836 }
 28837 
 28838 /* Calculate a width or height in pixels from a specification using
 28839    the following elements:
 28840 
 28841    SPEC ::=
 28842      NUM      - a (fractional) multiple of the default font width/height
 28843      (NUM)    - specifies exactly NUM pixels
 28844      UNIT     - a fixed number of pixels, see below.
 28845      ELEMENT  - size of a display element in pixels, see below.
 28846      (NUM . SPEC) - equals NUM * SPEC
 28847      (+ SPEC SPEC ...)  - add pixel values
 28848      (- SPEC SPEC ...)  - subtract pixel values
 28849      (- SPEC)           - negate pixel value
 28850 
 28851    NUM ::=
 28852      INT or FLOAT   - a number constant
 28853      SYMBOL         - use symbol's (buffer local) variable binding.
 28854 
 28855    UNIT ::=
 28856      in       - pixels per inch  *)
 28857      mm       - pixels per 1/1000 meter  *)
 28858      cm       - pixels per 1/100 meter   *)
 28859      width    - width of current font in pixels.
 28860      height   - height of current font in pixels.
 28861 
 28862      *) using the ratio(s) defined in display-pixels-per-inch.
 28863 
 28864    ELEMENT ::=
 28865 
 28866      left-fringe          - left fringe width in pixels
 28867      right-fringe         - right fringe width in pixels
 28868 
 28869      left-margin          - left margin width in pixels
 28870      right-margin         - right margin width in pixels
 28871 
 28872      scroll-bar           - scroll-bar area width in pixels
 28873 
 28874    Examples:
 28875 
 28876    Pixels corresponding to 5 inches:
 28877      (5 . in)
 28878 
 28879    Total width of non-text areas on left side of window (if scroll-bar is on left):
 28880      '(space :width (+ left-fringe left-margin scroll-bar))
 28881 
 28882    Align to first text column (in header line):
 28883      '(space :align-to 0)
 28884 
 28885    Align to middle of text area minus half the width of variable `my-image'
 28886    containing a loaded image:
 28887      '(space :align-to (0.5 . (- text my-image)))
 28888 
 28889    Width of left margin minus width of 1 character in the default font:
 28890      '(space :width (- left-margin 1))
 28891 
 28892    Width of left margin minus width of 2 characters in the current font:
 28893      '(space :width (- left-margin (2 . width)))
 28894 
 28895    Center 1 character over left-margin (in header line):
 28896      '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
 28897 
 28898    Different ways to express width of left fringe plus left margin minus one pixel:
 28899      '(space :width (- (+ left-fringe left-margin) (1)))
 28900      '(space :width (+ left-fringe left-margin (- (1))))
 28901      '(space :width (+ left-fringe left-margin (-1)))
 28902 
 28903    If ALIGN_TO is NULL, returns the result in *RES.  If ALIGN_TO is
 28904    non-NULL, the value of *ALIGN_TO is a window-relative pixel
 28905    coordinate, and *RES is the additional pixel width from that point
 28906    till the end of the stretch glyph.
 28907 
 28908    WIDTH_P non-zero means take the width dimension or X coordinate of
 28909    the object specified by PROP, WIDTH_P zero means take the height
 28910    dimension or the Y coordinate.  (Therefore, if ALIGN_TO is
 28911    non-NULL, WIDTH_P should be non-zero.)
 28912 
 28913    FONT is the font of the face of the surrounding text.
 28914 
 28915    The return value is non-zero if width or height were successfully
 28916    calculated, i.e. if PROP is a valid spec.  */
 28917 
 28918 static bool
 28919 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
 28920                             struct font *font, bool width_p, int *align_to)
 28921 {
 28922   /* Don't adjust for line number if we didn't yet produce it for this
 28923      screen line.  This is for when this function is called from
 28924      move_it_in_display_line_to that was called by display_line to get
 28925      past the glyphs hscrolled off the left side of the window.  */
 28926   int lnum_pixel_width = it->line_number_produced_p ? it->lnum_pixel_width : 0;
 28927   double pixels;
 28928 
 28929 # define OK_PIXELS(val) (*res = (val), true)
 28930 # define OK_ALIGN_TO(val) (*align_to = (val), true)
 28931 
 28932   if (NILP (prop))
 28933     return OK_PIXELS (0);
 28934 
 28935   eassert (FRAME_LIVE_P (it->f));
 28936 
 28937   if (SYMBOLP (prop))
 28938     {
 28939       if (SCHARS (SYMBOL_NAME (prop)) == 2)
 28940         {
 28941           char *unit = SSDATA (SYMBOL_NAME (prop));
 28942 
 28943           /* The UNIT expression, e.g. as part of (NUM . UNIT).  */
 28944           if (unit[0] == 'i' && unit[1] == 'n')
 28945             pixels = 1.0;
 28946           else if (unit[0] == 'm' && unit[1] == 'm')
 28947             pixels = 25.4;
 28948           else if (unit[0] == 'c' && unit[1] == 'm')
 28949             pixels = 2.54;
 28950           else
 28951             pixels = 0;
 28952           if (pixels > 0)
 28953             {
 28954               double ppi = (width_p ? FRAME_RES_X (it->f)
 28955                             : FRAME_RES_Y (it->f));
 28956 
 28957               if (ppi > 0)
 28958                 return OK_PIXELS (ppi / pixels);
 28959               return false;
 28960             }
 28961         }
 28962 
 28963 #ifdef HAVE_WINDOW_SYSTEM
 28964       /* 'height': the height of FONT.  */
 28965       if (EQ (prop, Qheight))
 28966         return OK_PIXELS (font
 28967                           ? normal_char_height (font, -1)
 28968                           : FRAME_LINE_HEIGHT (it->f));
 28969       /* 'width': the width of FONT.  */
 28970       if (EQ (prop, Qwidth))
 28971         return OK_PIXELS (font
 28972                           ? FONT_WIDTH (font)
 28973                           : FRAME_COLUMN_WIDTH (it->f));
 28974 #else
 28975       if (EQ (prop, Qheight) || EQ (prop, Qwidth))
 28976         return OK_PIXELS (1);
 28977 #endif
 28978 
 28979       /* 'text': the width or height of the text area.  */
 28980       if (EQ (prop, Qtext))
 28981           return OK_PIXELS (width_p
 28982                             ? (window_box_width (it->w, TEXT_AREA)
 28983                                - lnum_pixel_width)
 28984                             : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
 28985 
 28986       /* ':align_to'.  First time we compute the value, window
 28987          elements are interpreted as the position of the element's
 28988          left edge.  */
 28989       if (align_to && *align_to < 0)
 28990         {
 28991           *res = 0;
 28992           /* 'left': left edge of the text area.  */
 28993           if (EQ (prop, Qleft))
 28994             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 28995                                 + lnum_pixel_width);
 28996           /* 'right': right edge of the text area.  */
 28997           if (EQ (prop, Qright))
 28998             return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
 28999           /* 'center': the center of the text area.  */
 29000           if (EQ (prop, Qcenter))
 29001             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29002                                 + lnum_pixel_width
 29003                                 + window_box_width (it->w, TEXT_AREA) / 2);
 29004           /* 'left-fringe': left edge of the left fringe.  */
 29005           if (EQ (prop, Qleft_fringe))
 29006             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29007                                 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
 29008                                 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
 29009           /* 'right-fringe': left edge of the right fringe.  */
 29010           if (EQ (prop, Qright_fringe))
 29011             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29012                                 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29013                                 : window_box_right_offset (it->w, TEXT_AREA));
 29014           /* 'left-margin': left edge of the left display margin.  */
 29015           if (EQ (prop, Qleft_margin))
 29016             return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
 29017           /* 'right-margin': left edge of the right display margin.  */
 29018           if (EQ (prop, Qright_margin))
 29019             return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
 29020           /* 'scroll-bar': left edge of the vertical scroll bar.  */
 29021           if (EQ (prop, Qscroll_bar))
 29022             return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
 29023                                 ? 0
 29024                                 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29025                                    + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29026                                       ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29027                                       : 0)));
 29028         }
 29029       else
 29030         {
 29031           /* Otherwise, the elements stand for their width.  */
 29032           if (EQ (prop, Qleft_fringe))
 29033             return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
 29034           if (EQ (prop, Qright_fringe))
 29035             return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
 29036           if (EQ (prop, Qleft_margin))
 29037             return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
 29038           if (EQ (prop, Qright_margin))
 29039             return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
 29040           if (EQ (prop, Qscroll_bar))
 29041             return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
 29042         }
 29043 
 29044       prop = buffer_local_value (prop, it->w->contents);
 29045       if (BASE_EQ (prop, Qunbound))
 29046         prop = Qnil;
 29047     }
 29048 
 29049   if (NUMBERP (prop))
 29050     {
 29051       int base_unit = (width_p
 29052                        ? FRAME_COLUMN_WIDTH (it->f)
 29053                        : FRAME_LINE_HEIGHT (it->f));
 29054       if (width_p && align_to && *align_to < 0)
 29055         return OK_PIXELS (XFLOATINT (prop) * base_unit + lnum_pixel_width);
 29056       return OK_PIXELS (XFLOATINT (prop) * base_unit);
 29057     }
 29058 
 29059   if (CONSP (prop))
 29060     {
 29061       Lisp_Object car = XCAR (prop);
 29062       Lisp_Object cdr = XCDR (prop);
 29063 
 29064       if (SYMBOLP (car))
 29065         {
 29066 #ifdef HAVE_WINDOW_SYSTEM
 29067           /* '(image PROPS...)': width or height of the specified image.  */
 29068           if (FRAME_WINDOW_P (it->f)
 29069               && valid_image_p (prop))
 29070             {
 29071               ptrdiff_t id = lookup_image (it->f, prop, it->face_id);
 29072               struct image *img = IMAGE_FROM_ID (it->f, id);
 29073 
 29074               return OK_PIXELS (width_p ? img->width : img->height);
 29075             }
 29076           /* '(xwidget PROPS...)': dimensions of the specified xwidget.  */
 29077           if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
 29078             {
 29079               /* TODO: Don't return dummy size.  */
 29080               return OK_PIXELS (100);
 29081             }
 29082 #endif
 29083           /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
 29084              recursively calculated values.  */
 29085           if (EQ (car, Qplus) || EQ (car, Qminus))
 29086             {
 29087               bool first = true;
 29088               double px;
 29089 
 29090               pixels = 0;
 29091               while (CONSP (cdr))
 29092                 {
 29093                   if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
 29094                                                    font, width_p, align_to))
 29095                     return false;
 29096                   if (first)
 29097                     pixels = (EQ (car, Qplus) ? px : -px), first = false;
 29098                   else
 29099                     pixels += px;
 29100                   cdr = XCDR (cdr);
 29101                 }
 29102               if (EQ (car, Qminus))
 29103                 pixels = -pixels;
 29104               return OK_PIXELS (pixels);
 29105             }
 29106 
 29107           car = buffer_local_value (car, it->w->contents);
 29108           if (BASE_EQ (car, Qunbound))
 29109             car = Qnil;
 29110         }
 29111 
 29112       /* '(NUM)': absolute number of pixels.  */
 29113       if (NUMBERP (car))
 29114         {
 29115           double fact;
 29116           int offset =
 29117             width_p && align_to && *align_to < 0 ? lnum_pixel_width : 0;
 29118           pixels = XFLOATINT (car);
 29119           if (NILP (cdr))
 29120             return OK_PIXELS (pixels + offset);
 29121           if (calc_pixel_width_or_height (&fact, it, cdr,
 29122                                           font, width_p, align_to))
 29123             return OK_PIXELS (pixels * fact + offset);
 29124           return false;
 29125         }
 29126 
 29127       return false;
 29128     }
 29129 
 29130   return false;
 29131 }
 29132 
 29133 void
 29134 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
 29135 {
 29136 #ifdef HAVE_WINDOW_SYSTEM
 29137   normal_char_ascent_descent (font, -1, ascent, descent);
 29138 #else
 29139   *ascent = 1;
 29140   *descent = 0;
 29141 #endif
 29142 }
 29143 
 29144 
 29145 /***********************************************************************
 29146                              Glyph Display
 29147  ***********************************************************************/
 29148 
 29149 #ifdef HAVE_WINDOW_SYSTEM
 29150 
 29151 #ifdef GLYPH_DEBUG
 29152 
 29153 extern void dump_glyph_string (struct glyph_string *) EXTERNALLY_VISIBLE;
 29154 void
 29155 dump_glyph_string (struct glyph_string *s)
 29156 {
 29157   fputs ("glyph string\n", stderr);
 29158   fprintf (stderr, "  x, y, w, h = %d, %d, %d, %d\n",
 29159            s->x, s->y, s->width, s->height);
 29160   fprintf (stderr, "  ybase = %d\n", s->ybase);
 29161   fprintf (stderr, "  hl = %u\n", s->hl);
 29162   fprintf (stderr, "  left overhang = %d, right = %d\n",
 29163            s->left_overhang, s->right_overhang);
 29164   fprintf (stderr, "  nchars = %d\n", s->nchars);
 29165   fprintf (stderr, "  extends to end of line = %d\n",
 29166            s->extends_to_end_of_line_p);
 29167   fprintf (stderr, "  font height = %d\n", FONT_HEIGHT (s->font));
 29168   fprintf (stderr, "  bg width = %d\n", s->background_width);
 29169 }
 29170 
 29171 #endif /* GLYPH_DEBUG */
 29172 
 29173 /* Initialize glyph string S.  CHAR2B is a suitably allocated vector
 29174    of 2-byte unsigned integers for S; it can't be allocated in
 29175    init_glyph_string because it must be allocated via `alloca'.  W
 29176    is the window on which S is drawn.  ROW and AREA are the glyph row
 29177    and area within the row from which S is constructed.  START is the
 29178    index of the first glyph structure covered by S.  HL is a
 29179    face-override for drawing S.  */
 29180 
 29181 #ifdef HAVE_NTGUI
 29182 /* We set inhibit-quit here due to paranoia: get_frame_dc acquires the
 29183    critical section, and we cannot QUIT while we hold the critical
 29184    section.  If any of the code run by callers of ALLOCATE_HDC happens
 29185    to call Lisp (might be possible due to all the hooks lying around),
 29186    we must prevent it from quitting.  */
 29187 # define ALLOCATE_HDC(hdc, f)                   \
 29188   Lisp_Object prev_quit = Vinhibit_quit;        \
 29189   Vinhibit_quit = Qt;                           \
 29190   HDC hdc = get_frame_dc ((f))
 29191 # define RELEASE_HDC(hdc, f)                    \
 29192   release_frame_dc ((f), (hdc));                \
 29193   Vinhibit_quit = prev_quit
 29194 #else
 29195 # define ALLOCATE_HDC(hdc, f)
 29196 # define RELEASE_HDC(hdc, f)
 29197 #endif
 29198 
 29199 static void
 29200 init_glyph_string (struct glyph_string *s,
 29201 #ifdef HAVE_NTGUI
 29202                    HDC hdc,
 29203 #endif
 29204                    unsigned *char2b, struct window *w, struct glyph_row *row,
 29205                    enum glyph_row_area area, int start, enum draw_glyphs_face hl)
 29206 {
 29207   memset (s, 0, sizeof *s);
 29208   s->w = w;
 29209   s->f = XFRAME (w->frame);
 29210 #ifdef HAVE_NTGUI
 29211   s->hdc = hdc;
 29212 #endif
 29213   s->char2b = char2b;
 29214   s->hl = hl;
 29215   s->row = row;
 29216   s->area = area;
 29217   s->first_glyph = row->glyphs[area] + start;
 29218   s->height = row->height;
 29219   s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
 29220   s->ybase = s->y + row->ascent;
 29221 }
 29222 
 29223 
 29224 /* Append the list of glyph strings with head H and tail T to the list
 29225    with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the result.  */
 29226 
 29227 static void
 29228 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29229                            struct glyph_string *h, struct glyph_string *t)
 29230 {
 29231   if (h)
 29232     {
 29233       if (*head)
 29234         (*tail)->next = h;
 29235       else
 29236         *head = h;
 29237       h->prev = *tail;
 29238       *tail = t;
 29239     }
 29240 }
 29241 
 29242 
 29243 /* Prepend the list of glyph strings with head H and tail T to the
 29244    list with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the
 29245    result.  */
 29246 
 29247 static void
 29248 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29249                             struct glyph_string *h, struct glyph_string *t)
 29250 {
 29251   if (h)
 29252     {
 29253       if (*head)
 29254         (*head)->prev = t;
 29255       else
 29256         *tail = t;
 29257       t->next = *head;
 29258       *head = h;
 29259     }
 29260 }
 29261 
 29262 
 29263 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
 29264    Set *HEAD and *TAIL to the resulting list.  */
 29265 
 29266 static void
 29267 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
 29268                      struct glyph_string *s)
 29269 {
 29270   s->next = s->prev = NULL;
 29271   append_glyph_string_lists (head, tail, s, s);
 29272 }
 29273 
 29274 
 29275 /* Get face and two-byte form of character C in face FACE_ID on frame F.
 29276    The encoding of C is returned in *CHAR2B.  DISPLAY_P means
 29277    make sure that X resources for the face returned are allocated.
 29278    Value is a pointer to a realized face that is ready for display if
 29279    DISPLAY_P.  */
 29280 
 29281 static struct face *
 29282 get_char_face_and_encoding (struct frame *f, int c, int face_id,
 29283                             unsigned *char2b, bool display_p)
 29284 {
 29285   struct face *face = FACE_FROM_ID (f, face_id);
 29286   unsigned code = 0;
 29287 
 29288   if (face->font)
 29289     {
 29290       code = face->font->driver->encode_char (face->font, c);
 29291 
 29292       if (code == FONT_INVALID_CODE)
 29293         code = 0;
 29294     }
 29295   /* Ensure that the code is only 2 bytes wide.  */
 29296   *char2b = code & 0xFFFF;
 29297 
 29298   /* Make sure X resources of the face are allocated.  */
 29299 #ifdef HAVE_X_WINDOWS
 29300   if (display_p)
 29301 #endif
 29302     {
 29303       eassert (face != NULL);
 29304       prepare_face_for_display (f, face);
 29305     }
 29306 
 29307   return face;
 29308 }
 29309 
 29310 
 29311 /* Get face and two-byte form of character glyph GLYPH on frame F.
 29312    The encoding of GLYPH->u.ch is returned in *CHAR2B.  Value is
 29313    a pointer to a realized face that is ready for display.  */
 29314 
 29315 static struct face *
 29316 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
 29317                              unsigned *char2b)
 29318 {
 29319   struct face *face;
 29320   unsigned code = 0;
 29321 
 29322   eassert (glyph->type == CHAR_GLYPH);
 29323   face = FACE_FROM_ID (f, glyph->face_id);
 29324 
 29325   /* Make sure X resources of the face are allocated.  */
 29326   prepare_face_for_display (f, face);
 29327 
 29328   if (face->font)
 29329     {
 29330       if (CHAR_BYTE8_P (glyph->u.ch))
 29331         code = CHAR_TO_BYTE8 (glyph->u.ch);
 29332       else
 29333         code = face->font->driver->encode_char (face->font, glyph->u.ch);
 29334 
 29335       if (code == FONT_INVALID_CODE)
 29336         code = 0;
 29337     }
 29338 
 29339   /* Ensure that the code is only 2 bytes wide.  */
 29340   *char2b = code & 0xFFFF;
 29341   return face;
 29342 }
 29343 
 29344 
 29345 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
 29346    Return true iff FONT has a glyph for C.  */
 29347 
 29348 static bool
 29349 get_char_glyph_code (int c, struct font *font, unsigned *char2b)
 29350 {
 29351   unsigned code;
 29352 
 29353   if (CHAR_BYTE8_P (c))
 29354     code = CHAR_TO_BYTE8 (c);
 29355   else
 29356     code = font->driver->encode_char (font, c);
 29357 
 29358   if (code == FONT_INVALID_CODE)
 29359     return false;
 29360 
 29361   /* Ensure that the code is only 2 bytes wide.  */
 29362   *char2b = code & 0xFFFF;
 29363   return true;
 29364 }
 29365 
 29366 
 29367 /* Fill glyph string S with composition components specified by S->cmp.
 29368 
 29369    BASE_FACE is the base face of the composition.
 29370    S->cmp_from is the index of the first component for S.
 29371 
 29372    OVERLAPS non-zero means S should draw the foreground only, and use
 29373    its physical height for clipping.  See also draw_glyphs.
 29374 
 29375    Value is the index of a component not in S.  */
 29376 
 29377 static int
 29378 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
 29379                              int overlaps)
 29380 {
 29381   int i;
 29382   /* For all glyphs of this composition, starting at the offset
 29383      S->cmp_from, until we reach the end of the definition or encounter a
 29384      glyph that requires the different face, add it to S.  */
 29385   struct face *face;
 29386 
 29387   eassert (s);
 29388 
 29389   s->for_overlaps = overlaps;
 29390   s->face = NULL;
 29391   s->font = NULL;
 29392   for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
 29393     {
 29394       int c = COMPOSITION_GLYPH (s->cmp, i);
 29395 
 29396       /* TAB in a composition means display glyphs with padding space
 29397          on the left or right.  */
 29398       if (c != '\t')
 29399         {
 29400           int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
 29401                                        -1, Qnil);
 29402 
 29403           face = get_char_face_and_encoding (s->f, c, face_id,
 29404                                              s->char2b + i, true);
 29405           if (face)
 29406             {
 29407               if (! s->face)
 29408                 {
 29409                   s->face = face;
 29410                   s->font = s->face->font;
 29411                 }
 29412               else if (s->face != face)
 29413                 break;
 29414             }
 29415         }
 29416       ++s->nchars;
 29417     }
 29418   s->cmp_to = i;
 29419 
 29420   if (s->face == NULL)
 29421     {
 29422       s->face = base_face->ascii_face;
 29423       s->font = s->face->font;
 29424     }
 29425 
 29426   if (s->hl == DRAW_MOUSE_FACE
 29427       || (s->hl == DRAW_CURSOR
 29428           && MATRIX_ROW (s->w->current_matrix,
 29429                          s->w->phys_cursor.vpos)->mouse_face_p
 29430           && cursor_in_mouse_face_p (s->w)))
 29431     {
 29432       int c = COMPOSITION_GLYPH (s->cmp, 0);
 29433       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29434       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29435       if (!s->face)
 29436         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29437 
 29438       s->face = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face, c, -1, Qnil));
 29439       prepare_face_for_display (s->f, s->face);
 29440     }
 29441 
 29442   /* All glyph strings for the same composition has the same width,
 29443      i.e. the width set for the first component of the composition.  */
 29444   s->width = s->first_glyph->pixel_width;
 29445 
 29446   /* If the specified font could not be loaded, use the frame's
 29447      default font, but record the fact that we couldn't load it in
 29448      the glyph string so that we can draw rectangles for the
 29449      characters of the glyph string.  */
 29450   if (s->font == NULL)
 29451     {
 29452       s->font_not_found_p = true;
 29453       s->font = FRAME_FONT (s->f);
 29454     }
 29455 
 29456   /* Adjust base line for subscript/superscript text.  */
 29457   s->ybase += s->first_glyph->voffset;
 29458 
 29459   return s->cmp_to;
 29460 }
 29461 
 29462 static int
 29463 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
 29464                            int start, int end, int overlaps)
 29465 {
 29466   struct glyph *glyph, *last;
 29467   int voffset;
 29468   Lisp_Object lgstring;
 29469   int i;
 29470   bool glyph_not_available_p;
 29471 
 29472   s->for_overlaps = overlaps;
 29473   glyph = s->row->glyphs[s->area] + start;
 29474   last = s->row->glyphs[s->area] + end;
 29475   voffset = glyph->voffset;
 29476   glyph_not_available_p = glyph->glyph_not_available_p;
 29477   s->cmp_id = glyph->u.cmp.id;
 29478   s->cmp_from = glyph->slice.cmp.from;
 29479   s->cmp_to = glyph->slice.cmp.to + 1;
 29480   if (s->hl == DRAW_MOUSE_FACE
 29481       || (s->hl == DRAW_CURSOR
 29482           && MATRIX_ROW (s->w->current_matrix,
 29483                          s->w->phys_cursor.vpos)->mouse_face_p
 29484           && cursor_in_mouse_face_p (s->w)))
 29485     {
 29486       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29487       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29488       if (!s->face)
 29489         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29490       prepare_face_for_display (s->f, s->face);
 29491     }
 29492   else
 29493     s->face = FACE_FROM_ID (s->f, face_id);
 29494   lgstring = composition_gstring_from_id (s->cmp_id);
 29495   s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
 29496   /* The width of a composition glyph string is the sum of the
 29497      composition's glyph widths.  */
 29498   s->width = s->first_glyph->pixel_width;
 29499   glyph++;
 29500   while (glyph < last
 29501          && glyph->u.cmp.automatic
 29502          && glyph->u.cmp.id == s->cmp_id
 29503          && glyph->face_id == face_id
 29504          && s->cmp_to == glyph->slice.cmp.from
 29505          && glyph->glyph_not_available_p == glyph_not_available_p)
 29506     {
 29507       s->width += glyph->pixel_width;
 29508       s->cmp_to = (glyph++)->slice.cmp.to + 1;
 29509     }
 29510 
 29511   for (i = s->cmp_from; i < s->cmp_to; i++)
 29512     {
 29513       Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
 29514       unsigned code = LGLYPH_CODE (lglyph);
 29515 
 29516       /* Ensure that the code is only 2 bytes wide.  */
 29517       s->char2b[i] = code & 0xFFFF;
 29518     }
 29519 
 29520   /* If the specified font could not be loaded, record that fact in
 29521      S->font_not_found_p so that we can draw rectangles for the
 29522      characters of the glyph string.  */
 29523   if (glyph_not_available_p)
 29524     s->font_not_found_p = true;
 29525 
 29526   /* Adjust base line for subscript/superscript text.  */
 29527   s->ybase += voffset;
 29528 
 29529   return glyph - s->row->glyphs[s->area];
 29530 }
 29531 
 29532 
 29533 /* Fill glyph string S from a sequence glyphs for glyphless characters.
 29534    See the comment of fill_glyph_string for arguments.
 29535    Value is the index of the first glyph not in S.  */
 29536 
 29537 
 29538 static int
 29539 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
 29540                              int start, int end, int overlaps)
 29541 {
 29542   struct glyph *glyph, *last;
 29543   int voffset;
 29544 
 29545   eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
 29546   s->for_overlaps = overlaps;
 29547   glyph = s->row->glyphs[s->area] + start;
 29548   last = s->row->glyphs[s->area] + end;
 29549   voffset = glyph->voffset;
 29550   s->face = FACE_FROM_ID (s->f, face_id);
 29551   s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
 29552   if (s->hl == DRAW_MOUSE_FACE
 29553       || (s->hl == DRAW_CURSOR
 29554           && MATRIX_ROW (s->w->current_matrix,
 29555                          s->w->phys_cursor.vpos)->mouse_face_p
 29556           && cursor_in_mouse_face_p (s->w)))
 29557     {
 29558       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29559       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29560       if (!s->face)
 29561         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29562       prepare_face_for_display (s->f, s->face);
 29563     }
 29564   s->nchars = 1;
 29565   s->width = glyph->pixel_width;
 29566   glyph++;
 29567   while (glyph < last
 29568          && glyph->type == GLYPHLESS_GLYPH
 29569          && glyph->voffset == voffset
 29570          && glyph->face_id == face_id)
 29571     {
 29572       s->nchars++;
 29573       s->width += glyph->pixel_width;
 29574       glyph++;
 29575     }
 29576   s->ybase += voffset;
 29577   return glyph - s->row->glyphs[s->area];
 29578 }
 29579 
 29580 
 29581 /* Fill glyph string S from a sequence of character glyphs.
 29582 
 29583    FACE_ID is the face id of the string.  START is the index of the
 29584    first glyph to consider, END is the index of the last + 1.
 29585    OVERLAPS non-zero means S should draw the foreground only, and use
 29586    its physical height for clipping.  See also draw_glyphs.
 29587 
 29588    Value is the index of the first glyph not in S.  */
 29589 
 29590 static int
 29591 fill_glyph_string (struct glyph_string *s, int face_id,
 29592                    int start, int end, int overlaps)
 29593 {
 29594   struct glyph *glyph, *last;
 29595   int voffset;
 29596   bool glyph_not_available_p;
 29597 
 29598   eassert (s->f == XFRAME (s->w->frame));
 29599   eassert (s->nchars == 0);
 29600   eassert (start >= 0 && end > start);
 29601 
 29602   s->for_overlaps = overlaps;
 29603   glyph = s->row->glyphs[s->area] + start;
 29604   last = s->row->glyphs[s->area] + end;
 29605   voffset = glyph->voffset;
 29606   s->padding_p = glyph->padding_p;
 29607   glyph_not_available_p = glyph->glyph_not_available_p;
 29608 
 29609   while (glyph < last
 29610          && glyph->type == CHAR_GLYPH
 29611          && glyph->voffset == voffset
 29612          /* Same face id implies same font, nowadays.  */
 29613          && glyph->face_id == face_id
 29614          && glyph->glyph_not_available_p == glyph_not_available_p)
 29615     {
 29616       s->face = get_glyph_face_and_encoding (s->f, glyph,
 29617                                              s->char2b + s->nchars);
 29618       ++s->nchars;
 29619       eassert (s->nchars <= end - start);
 29620       s->width += glyph->pixel_width;
 29621       if (glyph++->padding_p != s->padding_p)
 29622         break;
 29623     }
 29624 
 29625   s->font = s->face->font;
 29626 
 29627   if (s->hl == DRAW_MOUSE_FACE
 29628       || (s->hl == DRAW_CURSOR
 29629           && MATRIX_ROW (s->w->current_matrix,
 29630                          s->w->phys_cursor.vpos)->mouse_face_p
 29631           && cursor_in_mouse_face_p (s->w)))
 29632     {
 29633       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29634       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29635       if (!s->face)
 29636         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29637       s->face
 29638         = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face,
 29639                                              s->first_glyph->u.ch, -1, Qnil));
 29640       prepare_face_for_display (s->f, s->face);
 29641     }
 29642 
 29643   /* If the specified font could not be loaded, use the frame's font,
 29644      but record the fact that we couldn't load it in
 29645      S->font_not_found_p so that we can draw rectangles for the
 29646      characters of the glyph string.  */
 29647   if (s->font == NULL || glyph_not_available_p)
 29648     {
 29649       s->font_not_found_p = true;
 29650       s->font = FRAME_FONT (s->f);
 29651     }
 29652 
 29653   /* Adjust base line for subscript/superscript text.  */
 29654   s->ybase += voffset;
 29655 
 29656   eassert (s->face && s->face->gc);
 29657   return glyph - s->row->glyphs[s->area];
 29658 }
 29659 
 29660 
 29661 /* Fill glyph string S from image glyph S->first_glyph.  */
 29662 
 29663 static void
 29664 fill_image_glyph_string (struct glyph_string *s)
 29665 {
 29666   eassert (s->first_glyph->type == IMAGE_GLYPH);
 29667   s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
 29668   eassert (s->img);
 29669   s->slice = s->first_glyph->slice.img;
 29670   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29671   s->font = s->face->font;
 29672   if (s->hl == DRAW_MOUSE_FACE
 29673       || (s->hl == DRAW_CURSOR
 29674           && MATRIX_ROW (s->w->current_matrix,
 29675                          s->w->phys_cursor.vpos)->mouse_face_p
 29676           && cursor_in_mouse_face_p (s->w)))
 29677     {
 29678       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29679       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29680       if (!s->face)
 29681         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29682       prepare_face_for_display (s->f, s->face);
 29683     }
 29684   s->width = s->first_glyph->pixel_width;
 29685 
 29686   /* Adjust base line for subscript/superscript text.  */
 29687   s->ybase += s->first_glyph->voffset;
 29688 }
 29689 
 29690 
 29691 #ifdef HAVE_XWIDGETS
 29692 static void
 29693 fill_xwidget_glyph_string (struct glyph_string *s)
 29694 {
 29695   eassert (s->first_glyph->type == XWIDGET_GLYPH);
 29696   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29697   s->font = s->face->font;
 29698   if (s->hl == DRAW_MOUSE_FACE
 29699       || (s->hl == DRAW_CURSOR
 29700           && MATRIX_ROW (s->w->current_matrix,
 29701                          s->w->phys_cursor.vpos)->mouse_face_p
 29702           && cursor_in_mouse_face_p (s->w)))
 29703     {
 29704       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29705       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29706       if (!s->face)
 29707         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29708       prepare_face_for_display (s->f, s->face);
 29709     }
 29710   s->width = s->first_glyph->pixel_width;
 29711   s->ybase += s->first_glyph->voffset;
 29712   s->xwidget = xwidget_from_id (s->first_glyph->u.xwidget);
 29713 }
 29714 #endif
 29715 /* Fill glyph string S from a sequence of stretch glyphs.
 29716 
 29717    START is the index of the first glyph to consider,
 29718    END is the index of the last + 1.
 29719 
 29720    Value is the index of the first glyph not in S.  */
 29721 
 29722 static int
 29723 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
 29724 {
 29725   struct glyph *glyph, *last;
 29726   int voffset, face_id;
 29727 
 29728   eassert (s->first_glyph->type == STRETCH_GLYPH);
 29729 
 29730   glyph = s->row->glyphs[s->area] + start;
 29731   last = s->row->glyphs[s->area] + end;
 29732   face_id = glyph->face_id;
 29733   s->face = FACE_FROM_ID (s->f, face_id);
 29734   s->font = s->face->font;
 29735   if (s->hl == DRAW_MOUSE_FACE
 29736       || (s->hl == DRAW_CURSOR
 29737           && MATRIX_ROW (s->w->current_matrix,
 29738                          s->w->phys_cursor.vpos)->mouse_face_p
 29739           && cursor_in_mouse_face_p (s->w)))
 29740     {
 29741       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29742       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29743       if (!s->face)
 29744         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29745       prepare_face_for_display (s->f, s->face);
 29746     }
 29747   s->width = glyph->pixel_width;
 29748   s->nchars = 1;
 29749   voffset = glyph->voffset;
 29750 
 29751   for (++glyph;
 29752        (glyph < last
 29753         && glyph->type == STRETCH_GLYPH
 29754         && glyph->voffset == voffset
 29755         && glyph->face_id == face_id);
 29756        ++glyph)
 29757     s->width += glyph->pixel_width;
 29758 
 29759   /* Adjust base line for subscript/superscript text.  */
 29760   s->ybase += voffset;
 29761 
 29762   /* The case that face->gc == 0 is handled when drawing the glyph
 29763      string by calling prepare_face_for_display.  */
 29764   eassert (s->face);
 29765   return glyph - s->row->glyphs[s->area];
 29766 }
 29767 
 29768 static struct font_metrics *
 29769 get_per_char_metric (struct font *font, const unsigned *char2b)
 29770 {
 29771   static struct font_metrics metrics;
 29772 
 29773   if (! font)
 29774     return NULL;
 29775   if (*char2b == FONT_INVALID_CODE)
 29776     return NULL;
 29777 
 29778   font->driver->text_extents (font, char2b, 1, &metrics);
 29779   return &metrics;
 29780 }
 29781 
 29782 /* A subroutine that computes "normal" values of ASCENT and DESCENT
 29783    for FONT.  Values are taken from font-global ones, except for fonts
 29784    that claim preposterously large values, but whose glyphs actually
 29785    have reasonable dimensions.  C is the character to use for metrics
 29786    if the font-global values are too large; if C is negative, the
 29787    function selects a default character.  */
 29788 static void
 29789 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
 29790 {
 29791   *ascent = FONT_BASE (font);
 29792   *descent = FONT_DESCENT (font);
 29793 
 29794   if (FONT_TOO_HIGH (font))
 29795     {
 29796       unsigned char2b;
 29797 
 29798       /* Get metrics of C, defaulting to a reasonably sized ASCII
 29799          character.  */
 29800       if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
 29801         {
 29802           struct font_metrics *pcm = get_per_char_metric (font, &char2b);
 29803           eassume (pcm);
 29804 
 29805           if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
 29806             {
 29807               /* We add 1 pixel to character dimensions as heuristics
 29808                  that produces nicer display, e.g. when the face has
 29809                  the box attribute.  */
 29810               *ascent = pcm->ascent + 1;
 29811               *descent = pcm->descent + 1;
 29812             }
 29813         }
 29814     }
 29815 }
 29816 
 29817 /* A subroutine that computes a reasonable "normal character height"
 29818    for fonts that claim preposterously large vertical dimensions, but
 29819    whose glyphs are actually reasonably sized.  C is the character
 29820    whose metrics to use for those fonts, or -1 for default
 29821    character.  */
 29822 static int
 29823 normal_char_height (struct font *font, int c)
 29824 {
 29825   int ascent, descent;
 29826 
 29827   normal_char_ascent_descent (font, c, &ascent, &descent);
 29828 
 29829   return ascent + descent;
 29830 }
 29831 
 29832 /* EXPORT for RIF:
 29833    Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
 29834    frame F.  Overhangs of glyphs other than type CHAR_GLYPH are
 29835    assumed to be zero.  */
 29836 
 29837 void
 29838 gui_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
 29839 {
 29840   *left = *right = 0;
 29841 
 29842   if (glyph->type == CHAR_GLYPH)
 29843     {
 29844       unsigned char2b;
 29845       struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
 29846       if (face->font)
 29847         {
 29848           struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
 29849           if (pcm)
 29850             {
 29851               if (pcm->rbearing > pcm->width)
 29852                 *right = pcm->rbearing - pcm->width;
 29853               if (pcm->lbearing < 0)
 29854                 *left = -pcm->lbearing;
 29855             }
 29856         }
 29857     }
 29858   else if (glyph->type == COMPOSITE_GLYPH)
 29859     {
 29860       if (! glyph->u.cmp.automatic)
 29861         {
 29862           struct composition *cmp = composition_table[glyph->u.cmp.id];
 29863 
 29864           if (cmp->rbearing > cmp->pixel_width)
 29865             *right = cmp->rbearing - cmp->pixel_width;
 29866           if (cmp->lbearing < 0)
 29867             *left = - cmp->lbearing;
 29868         }
 29869       else
 29870         {
 29871           Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
 29872           struct font_metrics metrics;
 29873 
 29874           composition_gstring_width (gstring, glyph->slice.cmp.from,
 29875                                      glyph->slice.cmp.to + 1, &metrics);
 29876           if (metrics.rbearing > metrics.width)
 29877             *right = metrics.rbearing - metrics.width;
 29878           if (metrics.lbearing < 0)
 29879             *left = - metrics.lbearing;
 29880         }
 29881     }
 29882 }
 29883 
 29884 
 29885 /* Return the index of the first glyph preceding glyph string S that
 29886    is overwritten by S because of S's left overhang.  Value is -1
 29887    if no glyphs are overwritten.  */
 29888 
 29889 static int
 29890 left_overwritten (struct glyph_string *s)
 29891 {
 29892   int k;
 29893 
 29894   if (s->left_overhang)
 29895     {
 29896       int x = 0, i;
 29897       struct glyph *glyphs = s->row->glyphs[s->area];
 29898       int first = s->first_glyph - glyphs;
 29899 
 29900       for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
 29901         x -= glyphs[i].pixel_width;
 29902 
 29903       k = i + 1;
 29904     }
 29905   else
 29906     k = -1;
 29907 
 29908   return k;
 29909 }
 29910 
 29911 
 29912 /* Return the index of the first glyph preceding glyph string S that
 29913    is overwriting S because of its right overhang.  Value is -1 if no
 29914    glyph in front of S overwrites S.  */
 29915 
 29916 static int
 29917 left_overwriting (struct glyph_string *s)
 29918 {
 29919   int i, k, x;
 29920   struct glyph *glyphs = s->row->glyphs[s->area];
 29921   int first = s->first_glyph - glyphs;
 29922 
 29923   k = -1;
 29924   x = 0;
 29925   for (i = first - 1; i >= 0; --i)
 29926     {
 29927       int left, right;
 29928       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 29929       if (x + right > 0)
 29930         k = i;
 29931       x -= glyphs[i].pixel_width;
 29932     }
 29933 
 29934   return k;
 29935 }
 29936 
 29937 
 29938 /* Return the index of the last glyph following glyph string S that is
 29939    overwritten by S because of S's right overhang.  Value is -1 if
 29940    no such glyph is found.  */
 29941 
 29942 static int
 29943 right_overwritten (struct glyph_string *s)
 29944 {
 29945   int k = -1;
 29946 
 29947   if (s->right_overhang)
 29948     {
 29949       int x = 0, i;
 29950       struct glyph *glyphs = s->row->glyphs[s->area];
 29951       int first = (s->first_glyph - glyphs
 29952                    + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 29953       int end = s->row->used[s->area];
 29954 
 29955       for (i = first; i < end && s->right_overhang > x; ++i)
 29956         x += glyphs[i].pixel_width;
 29957 
 29958       k = i;
 29959     }
 29960 
 29961   return k;
 29962 }
 29963 
 29964 
 29965 /* Return the index of the last glyph following glyph string S that
 29966    overwrites S because of its left overhang.  Value is negative
 29967    if no such glyph is found.  */
 29968 
 29969 static int
 29970 right_overwriting (struct glyph_string *s)
 29971 {
 29972   int i, k, x;
 29973   int end = s->row->used[s->area];
 29974   struct glyph *glyphs = s->row->glyphs[s->area];
 29975   int first = (s->first_glyph - glyphs
 29976                + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 29977 
 29978   k = -1;
 29979   x = 0;
 29980   for (i = first; i < end; ++i)
 29981     {
 29982       int left, right;
 29983       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 29984       if (x - left < 0)
 29985         k = i;
 29986       x += glyphs[i].pixel_width;
 29987     }
 29988 
 29989   return k;
 29990 }
 29991 
 29992 
 29993 /* Set background width of glyph string S.  START is the index of the
 29994    first glyph following S.  LAST_X is the right-most x-position + 1
 29995    in the drawing area.
 29996 
 29997    If S->hl is DRAW_CURSOR, S->f is a window system frame, and the
 29998    cursor in S's window is currently inside mouse face, also update
 29999    S->width to take into account potentially differing :box
 30000    properties between the original face and the mouse face.  */
 30001 
 30002 static void
 30003 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
 30004 {
 30005   /* If the face of this glyph string has to be drawn to the end of
 30006      the drawing area, set S->extends_to_end_of_line_p.  */
 30007 
 30008   if (start == s->row->used[s->area]
 30009       && ((s->row->fill_line_p
 30010            && (s->hl == DRAW_NORMAL_TEXT
 30011                || s->hl == DRAW_IMAGE_RAISED
 30012                || s->hl == DRAW_IMAGE_SUNKEN))
 30013           || s->hl == DRAW_MOUSE_FACE))
 30014     s->extends_to_end_of_line_p = true;
 30015 
 30016   /* If S extends its face to the end of the line, set its
 30017      background_width to the distance to the right edge of the drawing
 30018      area.  */
 30019   if (s->extends_to_end_of_line_p)
 30020     s->background_width = last_x - s->x + 1;
 30021   else
 30022     {
 30023       s->background_width = s->width;
 30024 #ifdef HAVE_WINDOW_SYSTEM
 30025       if (FRAME_WINDOW_P (s->f)
 30026           && s->hl == DRAW_CURSOR
 30027           && MATRIX_ROW (s->w->current_matrix,
 30028                          s->w->phys_cursor.vpos)->mouse_face_p
 30029           && cursor_in_mouse_face_p (s->w))
 30030         {
 30031           /* Adjust the background width of the glyph string, because
 30032              if the glyph's face has the :box attribute, its
 30033              pixel_width might be different when it's displayed in the
 30034              mouse-face, if that also has the :box attribute.  */
 30035           struct glyph *g = s->first_glyph;
 30036           struct face *regular_face = FACE_FROM_ID (s->f, g->face_id);
 30037           s->background_width +=
 30038             adjust_glyph_width_for_mouse_face (g, s->row, s->w,
 30039                                                regular_face, s->face);
 30040           /* S->width is probably worth adjusting here as well.  */
 30041           s->width = s->background_width;
 30042         }
 30043 #endif
 30044     }
 30045 }
 30046 
 30047 
 30048 /* Return glyph string that shares background with glyph string S and
 30049    whose `background_width' member has been set.  */
 30050 
 30051 static struct glyph_string *
 30052 glyph_string_containing_background_width (struct glyph_string *s)
 30053 {
 30054   if (s->cmp)
 30055     while (s->cmp_from)
 30056       s = s->prev;
 30057 
 30058   return s;
 30059 }
 30060 
 30061 
 30062 /* Compute overhangs and x-positions for glyph string S and its
 30063    predecessors, or successors.  X is the starting x-position for S.
 30064    BACKWARD_P means process predecessors.  */
 30065 
 30066 static void
 30067 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
 30068 {
 30069   if (backward_p)
 30070     {
 30071       while (s)
 30072         {
 30073           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30074             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30075           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30076             x -= s->width;
 30077           s->x = x;
 30078           s = s->prev;
 30079         }
 30080     }
 30081   else
 30082     {
 30083       while (s)
 30084         {
 30085           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30086             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30087           s->x = x;
 30088           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30089             x += s->width;
 30090           s = s->next;
 30091         }
 30092     }
 30093 }
 30094 
 30095 
 30096 
 30097 /* The following macros are only called from draw_glyphs below.
 30098    They reference the following parameters of that function directly:
 30099      `w', `row', `area', and `overlap_p'
 30100    as well as the following local variables:
 30101      `s', `f', and `hdc' (in W32)  */
 30102 
 30103 #ifdef HAVE_NTGUI
 30104 /* On W32, silently add local `hdc' variable to argument list of
 30105    init_glyph_string.  */
 30106 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30107   init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
 30108 #else
 30109 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30110   init_glyph_string (s, char2b, w, row, area, start, hl)
 30111 #endif
 30112 
 30113 /* Add a glyph string for a stretch glyph to the list of strings
 30114    between HEAD and TAIL.  START is the index of the stretch glyph in
 30115    row area AREA of glyph row ROW.  END is the index of the last glyph
 30116    in that glyph row area.  X is the current output position assigned
 30117    to the new glyph string constructed.  HL overrides that face of the
 30118    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30119    is the right-most x-position of the drawing area.  */
 30120 
 30121 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
 30122    and below -- keep them on one line.  */
 30123 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30124      do                                                                     \
 30125        {                                                                    \
 30126          s = alloca (sizeof *s);                                            \
 30127          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);              \
 30128          START = fill_stretch_glyph_string (s, START, END);                 \
 30129          append_glyph_string (&HEAD, &TAIL, s);                             \
 30130          s->x = (X);                                                        \
 30131        }                                                                    \
 30132      while (false)
 30133 
 30134 
 30135 /* Add a glyph string for an image glyph to the list of strings
 30136    between HEAD and TAIL.  START is the index of the image glyph in
 30137    row area AREA of glyph row ROW.  END is the index of the last glyph
 30138    in that glyph row area.  X is the current output position assigned
 30139    to the new glyph string constructed.  HL overrides that face of the
 30140    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30141    is the right-most x-position of the drawing area.  */
 30142 
 30143 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30144      do                                                                 \
 30145        {                                                                \
 30146          s = alloca (sizeof *s);                                        \
 30147          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30148          fill_image_glyph_string (s);                                   \
 30149          append_glyph_string (&HEAD, &TAIL, s);                         \
 30150          ++START;                                                       \
 30151          s->x = (X);                                                    \
 30152        }                                                                \
 30153      while (false)
 30154 
 30155 #ifndef HAVE_XWIDGETS
 30156 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30157      eassume (false)
 30158 #else
 30159 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30160      do                                                                 \
 30161        {                                                                \
 30162          s = alloca (sizeof *s);                                        \
 30163          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30164          fill_xwidget_glyph_string (s);                                 \
 30165          append_glyph_string (&(HEAD), &(TAIL), s);                     \
 30166          ++(START);                                                     \
 30167          s->x = (X);                                                    \
 30168        }                                                                \
 30169      while (false)
 30170 #endif
 30171 
 30172 /* Add a glyph string for a sequence of character glyphs to the list
 30173    of strings between HEAD and TAIL.  START is the index of the first
 30174    glyph in row area AREA of glyph row ROW that is part of the new
 30175    glyph string.  END is the index of the last glyph in that glyph row
 30176    area.  X is the current output position assigned to the new glyph
 30177    string constructed.  HL overrides that face of the glyph; e.g. it
 30178    is DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the
 30179    right-most x-position of the drawing area.  */
 30180 
 30181 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30182      do                                                                    \
 30183        {                                                                   \
 30184          int face_id;                                                      \
 30185          unsigned *char2b;                                         \
 30186                                                                            \
 30187          face_id = (row)->glyphs[area][START].face_id;                     \
 30188                                                                            \
 30189          s = alloca (sizeof *s);                                           \
 30190          SAFE_NALLOCA (char2b, 1, (END) - (START));                        \
 30191          INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);           \
 30192          append_glyph_string (&HEAD, &TAIL, s);                            \
 30193          s->x = (X);                                                       \
 30194          START = fill_glyph_string (s, face_id, START, END, overlaps);     \
 30195        }                                                                   \
 30196      while (false)
 30197 
 30198 
 30199 /* Add a glyph string for a composite sequence to the list of strings
 30200    between HEAD and TAIL.  START is the index of the first glyph in
 30201    row area AREA of glyph row ROW that is part of the new glyph
 30202    string.  END is the index of the last glyph in that glyph row area.
 30203    X is the current output position assigned to the new glyph string
 30204    constructed.  HL overrides that face of the glyph; e.g. it is
 30205    DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the right-most
 30206    x-position of the drawing area.  */
 30207 
 30208 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30209   do {                                                                      \
 30210     int face_id = (row)->glyphs[area][START].face_id;                       \
 30211     struct face *base_face = FACE_FROM_ID (f, face_id);             \
 30212     ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id;                 \
 30213     struct composition *cmp = composition_table[cmp_id];                    \
 30214     unsigned *char2b;                                                       \
 30215     struct glyph_string *first_s = NULL;                                    \
 30216     int n;                                                                  \
 30217                                                                             \
 30218     SAFE_NALLOCA (char2b, 1, cmp->glyph_len);                               \
 30219                                                                             \
 30220     /* Make glyph_strings for each glyph sequence that is drawable by       \
 30221        the same face, and append them to HEAD/TAIL.  */                     \
 30222     for (n = 0; n < cmp->glyph_len;)                                        \
 30223       {                                                                     \
 30224         s = alloca (sizeof *s);                                             \
 30225         INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);             \
 30226         append_glyph_string (&(HEAD), &(TAIL), s);                          \
 30227         s->cmp = cmp;                                                       \
 30228         s->cmp_from = n;                                                    \
 30229         s->x = (X);                                                         \
 30230         if (n == 0)                                                         \
 30231           first_s = s;                                                      \
 30232         n = fill_composite_glyph_string (s, base_face, overlaps);           \
 30233       }                                                                     \
 30234                                                                             \
 30235     ++START;                                                                \
 30236     s = first_s;                                                            \
 30237   } while (false)
 30238 
 30239 
 30240 /* Add a glyph string for a glyph-string sequence to the list of strings
 30241    between HEAD and TAIL.  */
 30242 
 30243 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30244   do {                                                                    \
 30245     int face_id;                                                          \
 30246     unsigned *char2b;                                                     \
 30247     Lisp_Object gstring;                                                  \
 30248                                                                           \
 30249     face_id = (row)->glyphs[area][START].face_id;                         \
 30250     gstring = (composition_gstring_from_id                                \
 30251                ((row)->glyphs[area][START].u.cmp.id));                    \
 30252     s = alloca (sizeof *s);                                               \
 30253     SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring));               \
 30254     INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);               \
 30255     append_glyph_string (&(HEAD), &(TAIL), s);                            \
 30256     s->x = (X);                                                           \
 30257     START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
 30258   } while (false)
 30259 
 30260 
 30261 /* Add a glyph string for a sequence of glyphless character's glyphs
 30262    to the list of strings between HEAD and TAIL.  The meanings of
 30263    arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS.  */
 30264 
 30265 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30266   do                                                                        \
 30267     {                                                                       \
 30268       int face_id;                                                          \
 30269                                                                             \
 30270       face_id = (row)->glyphs[area][START].face_id;                         \
 30271                                                                             \
 30272       s = alloca (sizeof *s);                                               \
 30273       INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);                 \
 30274       append_glyph_string (&HEAD, &TAIL, s);                                \
 30275       s->x = (X);                                                           \
 30276       START = fill_glyphless_glyph_string (s, face_id, START, END,          \
 30277                                            overlaps);                       \
 30278     }                                                                       \
 30279   while (false)
 30280 
 30281 
 30282 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
 30283    of AREA of glyph row ROW on window W between indices START and END.
 30284    HL overrides the face for drawing glyph strings, e.g. it is
 30285    DRAW_CURSOR to draw a cursor.  X and LAST_X are start and end
 30286    x-positions of the drawing area.
 30287 
 30288    This is an ugly monster macro construct because we must use alloca
 30289    to allocate glyph strings (because draw_glyphs can be called
 30290    asynchronously).  */
 30291 
 30292 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30293   do                                                                    \
 30294     {                                                                   \
 30295       HEAD = TAIL = NULL;                                               \
 30296       while (START < END)                                               \
 30297         {                                                               \
 30298           struct glyph *first_glyph = (row)->glyphs[area] + START;      \
 30299           switch (first_glyph->type)                                    \
 30300             {                                                           \
 30301             case CHAR_GLYPH:                                            \
 30302               BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL,         \
 30303                                         HL, X, LAST_X);                 \
 30304               break;                                                    \
 30305                                                                         \
 30306             case COMPOSITE_GLYPH:                                       \
 30307               if (first_glyph->u.cmp.automatic)                         \
 30308                 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30309                                             HL, X, LAST_X);             \
 30310               else                                                      \
 30311                 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL,   \
 30312                                               HL, X, LAST_X);           \
 30313               break;                                                    \
 30314                                                                         \
 30315             case STRETCH_GLYPH:                                         \
 30316               BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30317                                           HL, X, LAST_X);               \
 30318               break;                                                    \
 30319                                                                         \
 30320             case IMAGE_GLYPH:                                           \
 30321               BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL,         \
 30322                                         HL, X, LAST_X);                 \
 30323               break;
 30324 
 30325 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30326             case XWIDGET_GLYPH:                                         \
 30327               BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30328                                           HL, X, LAST_X);               \
 30329               break;
 30330 
 30331 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30332             case GLYPHLESS_GLYPH:                                       \
 30333               BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30334                                             HL, X, LAST_X);             \
 30335               break;                                                    \
 30336                                                                         \
 30337             default:                                                    \
 30338               emacs_abort ();                                           \
 30339             }                                                           \
 30340                                                                         \
 30341           if (s)                                                        \
 30342             {                                                           \
 30343               set_glyph_string_background_width (s, START, LAST_X);     \
 30344               (X) += s->width;                                          \
 30345             }                                                           \
 30346         }                                                               \
 30347     } while (false)
 30348 
 30349 
 30350 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)      \
 30351     BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)        \
 30352     BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)       \
 30353     BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
 30354 
 30355 
 30356 /* Draw glyphs between START and END in AREA of ROW on window W,
 30357    starting at x-position X.  X is relative to AREA in W.  HL is a
 30358    face-override with the following meaning:
 30359 
 30360    DRAW_NORMAL_TEXT     draw normally
 30361    DRAW_CURSOR          draw in cursor face
 30362    DRAW_MOUSE_FACE      draw in mouse face.
 30363    DRAW_INVERSE_VIDEO   draw in mode line face
 30364    DRAW_IMAGE_SUNKEN    draw an image with a sunken relief around it
 30365    DRAW_IMAGE_RAISED    draw an image with a raised relief around it
 30366 
 30367    If OVERLAPS is non-zero, draw only the foreground of characters and
 30368    clip to the physical height of ROW.  Non-zero value also defines
 30369    the overlapping part to be drawn:
 30370 
 30371    OVERLAPS_PRED                overlap with preceding rows
 30372    OVERLAPS_SUCC                overlap with succeeding rows
 30373    OVERLAPS_BOTH                overlap with both preceding/succeeding rows
 30374    OVERLAPS_ERASED_CURSOR       overlap with erased cursor area
 30375 
 30376    Value is the x-position reached, relative to AREA of W.  */
 30377 
 30378 static int
 30379 draw_glyphs (struct window *w, int x, struct glyph_row *row,
 30380              enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
 30381              enum draw_glyphs_face hl, int overlaps)
 30382 {
 30383   struct glyph_string *head, *tail;
 30384   struct glyph_string *s;
 30385   struct glyph_string *clip_head = NULL, *clip_tail = NULL;
 30386   int i, j, x_reached, last_x, area_left = 0;
 30387   struct frame *f = XFRAME (WINDOW_FRAME (w));
 30388 
 30389   ALLOCATE_HDC (hdc, f);
 30390 
 30391   /* Let's rather be paranoid than getting a SEGV.  */
 30392   end = min (end, row->used[area]);
 30393   start = clip_to_bounds (0, start, end);
 30394 
 30395   /* Translate X to frame coordinates.  Set last_x to the right
 30396      end of the drawing area.  */
 30397   if (row->full_width_p)
 30398     {
 30399       /* X is relative to the left edge of W, without scroll bars
 30400          or fringes.  */
 30401       area_left = WINDOW_LEFT_EDGE_X (w);
 30402       last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
 30403                 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 30404     }
 30405   else
 30406     {
 30407       area_left = window_box_left (w, area);
 30408       last_x = area_left + window_box_width (w, area);
 30409     }
 30410   x += area_left;
 30411 
 30412   /* Build a doubly-linked list of glyph_string structures between
 30413      head and tail from what we have to draw.  Note that the macro
 30414      BUILD_GLYPH_STRINGS will modify its start parameter.  That's
 30415      the reason we use a separate variable `i'.  */
 30416   i = start;
 30417   USE_SAFE_ALLOCA;
 30418   BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
 30419   if (tail)
 30420     {
 30421       s = glyph_string_containing_background_width (tail);
 30422       x_reached = s->x + s->background_width;
 30423     }
 30424   else
 30425     x_reached = x;
 30426 
 30427   /* If there are any glyphs with lbearing < 0 or rbearing > width in
 30428      the row, redraw some glyphs in front or following the glyph
 30429      strings built above.  */
 30430   if (head && !overlaps && row->contains_overlapping_glyphs_p)
 30431     {
 30432       struct glyph_string *h, *t;
 30433       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 30434       int mouse_beg_col UNINIT, mouse_end_col UNINIT;
 30435       bool check_mouse_face = false;
 30436       int dummy_x = 0;
 30437 
 30438       /* If mouse highlighting is on, we may need to draw adjacent
 30439          glyphs using mouse-face highlighting.  */
 30440       if (area == TEXT_AREA && row->mouse_face_p
 30441           && hlinfo->mouse_face_beg_row >= 0
 30442           && hlinfo->mouse_face_end_row >= 0)
 30443         {
 30444           ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
 30445 
 30446           if (row_vpos >= hlinfo->mouse_face_beg_row
 30447               && row_vpos <= hlinfo->mouse_face_end_row)
 30448             {
 30449               check_mouse_face = true;
 30450               mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
 30451                 ? hlinfo->mouse_face_beg_col : 0;
 30452               mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
 30453                 ? hlinfo->mouse_face_end_col
 30454                 : row->used[TEXT_AREA];
 30455             }
 30456         }
 30457 
 30458       /* Compute overhangs for all glyph strings.  */
 30459       if (FRAME_RIF (f)->compute_glyph_string_overhangs)
 30460         for (s = head; s; s = s->next)
 30461           FRAME_RIF (f)->compute_glyph_string_overhangs (s);
 30462 
 30463       /* Prepend glyph strings for glyphs in front of the first glyph
 30464          string that are overwritten because of the first glyph
 30465          string's left overhang.  The background of all strings
 30466          prepended must be drawn because the first glyph string
 30467          draws over it.  */
 30468       i = left_overwritten (head);
 30469       if (i >= 0)
 30470         {
 30471           enum draw_glyphs_face overlap_hl;
 30472 
 30473           /* If this row contains mouse highlighting, attempt to draw
 30474              the overlapped glyphs with the correct highlight.  This
 30475              code fails if the overlap encompasses more than one glyph
 30476              and mouse-highlight spans only some of these glyphs.
 30477              However, making it work perfectly involves a lot more
 30478              code, and I don't know if the pathological case occurs in
 30479              practice, so we'll stick to this for now.  --- cyd  */
 30480           if (check_mouse_face
 30481               && mouse_beg_col < start && mouse_end_col > i)
 30482             overlap_hl = DRAW_MOUSE_FACE;
 30483           else
 30484             overlap_hl = DRAW_NORMAL_TEXT;
 30485 
 30486           if (hl != overlap_hl)
 30487             clip_head = head;
 30488           j = i;
 30489           BUILD_GLYPH_STRINGS (j, start, h, t,
 30490                                overlap_hl, dummy_x, last_x);
 30491           start = i;
 30492           compute_overhangs_and_x (t, head->x, true);
 30493           prepend_glyph_string_lists (&head, &tail, h, t);
 30494           if (clip_head == NULL)
 30495             clip_head = head;
 30496         }
 30497 
 30498       /* Prepend glyph strings for glyphs in front of the first glyph
 30499          string that overwrite that glyph string because of their
 30500          right overhang.  For these strings, only the foreground must
 30501          be drawn, because it draws over the glyph string at `head'.
 30502          The background must not be drawn because this would overwrite
 30503          right overhangs of preceding glyphs for which no glyph
 30504          strings exist.  */
 30505       i = left_overwriting (head);
 30506       if (i >= 0)
 30507         {
 30508           enum draw_glyphs_face overlap_hl;
 30509 
 30510           if (check_mouse_face
 30511               && mouse_beg_col < start && mouse_end_col > i)
 30512             overlap_hl = DRAW_MOUSE_FACE;
 30513           else
 30514             overlap_hl = DRAW_NORMAL_TEXT;
 30515 
 30516           if (hl == overlap_hl || clip_head == NULL)
 30517             clip_head = head;
 30518           BUILD_GLYPH_STRINGS (i, start, h, t,
 30519                                overlap_hl, dummy_x, last_x);
 30520           for (s = h; s; s = s->next)
 30521             s->background_filled_p = true;
 30522           compute_overhangs_and_x (t, head->x, true);
 30523           prepend_glyph_string_lists (&head, &tail, h, t);
 30524         }
 30525 
 30526       /* Append glyphs strings for glyphs following the last glyph
 30527          string tail that are overwritten by tail.  The background of
 30528          these strings has to be drawn because tail's foreground draws
 30529          over it.  */
 30530       i = right_overwritten (tail);
 30531       if (i >= 0)
 30532         {
 30533           enum draw_glyphs_face overlap_hl;
 30534 
 30535           if (check_mouse_face
 30536               && mouse_beg_col < i && mouse_end_col > end)
 30537             overlap_hl = DRAW_MOUSE_FACE;
 30538           else
 30539             overlap_hl = DRAW_NORMAL_TEXT;
 30540 
 30541           if (hl != overlap_hl)
 30542             clip_tail = tail;
 30543           BUILD_GLYPH_STRINGS (end, i, h, t,
 30544                                overlap_hl, x, last_x);
 30545           /* Because BUILD_GLYPH_STRINGS updates the first argument,
 30546              we don't have `end = i;' here.  */
 30547           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30548           append_glyph_string_lists (&head, &tail, h, t);
 30549           if (clip_tail == NULL)
 30550             clip_tail = tail;
 30551         }
 30552 
 30553       /* Append glyph strings for glyphs following the last glyph
 30554          string tail that overwrite tail.  The foreground of such
 30555          glyphs has to be drawn because it writes into the background
 30556          of tail.  The background must not be drawn because it could
 30557          paint over the foreground of following glyphs.  */
 30558       i = right_overwriting (tail);
 30559       if (i >= 0)
 30560         {
 30561           enum draw_glyphs_face overlap_hl;
 30562           if (check_mouse_face
 30563               && mouse_beg_col < i && mouse_end_col > end)
 30564             overlap_hl = DRAW_MOUSE_FACE;
 30565           else
 30566             overlap_hl = DRAW_NORMAL_TEXT;
 30567 
 30568           if (hl == overlap_hl || clip_tail == NULL)
 30569             clip_tail = tail;
 30570           i++;                  /* We must include the Ith glyph.  */
 30571           BUILD_GLYPH_STRINGS (end, i, h, t,
 30572                                overlap_hl, x, last_x);
 30573           for (s = h; s; s = s->next)
 30574             s->background_filled_p = true;
 30575           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30576           append_glyph_string_lists (&head, &tail, h, t);
 30577         }
 30578       tail = glyph_string_containing_background_width (tail);
 30579       if (clip_tail)
 30580         clip_tail = glyph_string_containing_background_width (clip_tail);
 30581       if (clip_head || clip_tail)
 30582         for (s = head; s; s = s->next)
 30583           {
 30584             s->clip_head = clip_head;
 30585             s->clip_tail = clip_tail;
 30586           }
 30587     }
 30588 
 30589   /* Draw all strings.  */
 30590   for (s = head; s; s = s->next)
 30591     FRAME_RIF (f)->draw_glyph_string (s);
 30592 
 30593   /* When focus a sole frame and move horizontally, this clears on_p
 30594      causing a failure to erase prev cursor position. */
 30595   if (area == TEXT_AREA
 30596       && !row->full_width_p
 30597       /* When drawing overlapping rows, only the glyph strings'
 30598          foreground is drawn, which doesn't erase a cursor
 30599          completely. */
 30600       && !overlaps)
 30601     {
 30602       int x0 = clip_head ? clip_head->x : (head ? head->x : x);
 30603       int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
 30604                 : (tail ? tail->x + tail->background_width : x));
 30605       x0 -= area_left;
 30606       x1 -= area_left;
 30607 
 30608       notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
 30609                                  row->y, MATRIX_ROW_BOTTOM_Y (row));
 30610     }
 30611 
 30612   /* Value is the x-position up to which drawn, relative to AREA of W.
 30613      This doesn't include parts drawn because of overhangs.  */
 30614   if (row->full_width_p)
 30615     x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
 30616   else
 30617     x_reached -= area_left;
 30618 
 30619   RELEASE_HDC (hdc, f);
 30620 
 30621   SAFE_FREE ();
 30622   return x_reached;
 30623 }
 30624 
 30625 /* Find the first glyph in the run of underlined glyphs preceding the
 30626    beginning of glyph string S, and return its font (which could be
 30627    NULL).  This is needed because that font determines the underline
 30628    position and thickness for the entire run of the underlined glyphs.
 30629    This function is called from the draw_glyph_string method of GUI
 30630    frame's redisplay interface (RIF) when it needs to draw in an
 30631    underlined face.  */
 30632 struct font *
 30633 font_for_underline_metrics (struct glyph_string *s)
 30634 {
 30635   struct glyph *g0 = s->row->glyphs[s->area], *g;
 30636 
 30637   for (g = s->first_glyph - 1; g >= g0; g--)
 30638     {
 30639       struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
 30640       if (!(prev_face && prev_face->underline != FACE_NO_UNDERLINE))
 30641         break;
 30642     }
 30643 
 30644   /* If preceding glyphs are not underlined, use the font of S.  */
 30645   if (g == s->first_glyph - 1)
 30646     return s->font;
 30647   else
 30648     {
 30649       /* Otherwise use the font of the last glyph we saw in the above
 30650          loop whose face had the underline_p flag set.  */
 30651       return FACE_FROM_ID (s->f, g[1].face_id)->font;
 30652     }
 30653 }
 30654 
 30655 /* Expand row matrix if too narrow.  Don't expand if area
 30656    is not present.  */
 30657 
 30658 #define IT_EXPAND_MATRIX_WIDTH(it, area)                \
 30659   {                                                     \
 30660     if (!it->f->fonts_changed                           \
 30661         && (it->glyph_row->glyphs[area]                 \
 30662             < it->glyph_row->glyphs[area + 1]))         \
 30663       {                                                 \
 30664         it->w->ncols_scale_factor++;                    \
 30665         it->f->fonts_changed = true;                    \
 30666       }                                                 \
 30667   }
 30668 
 30669 /* Store one glyph for IT->char_to_display in IT->glyph_row.
 30670    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30671 
 30672 static void
 30673 append_glyph (struct it *it)
 30674 {
 30675   struct glyph *glyph;
 30676   enum glyph_row_area area = it->area;
 30677 
 30678   eassert (it->glyph_row);
 30679   eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
 30680 
 30681   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30682   if (glyph < it->glyph_row->glyphs[area + 1])
 30683     {
 30684       /* If the glyph row is reversed, we need to prepend the glyph
 30685          rather than append it.  */
 30686       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30687         {
 30688           struct glyph *g;
 30689 
 30690           /* Make room for the additional glyph.  */
 30691           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 30692             g[1] = *g;
 30693           glyph = it->glyph_row->glyphs[area];
 30694         }
 30695       glyph->charpos = CHARPOS (it->position);
 30696       glyph->object = it->object;
 30697       if (it->pixel_width > 0)
 30698         {
 30699           eassert (it->pixel_width <= SHRT_MAX);
 30700           glyph->pixel_width = it->pixel_width;
 30701           glyph->padding_p = false;
 30702         }
 30703       else
 30704         {
 30705           /* Assure at least 1-pixel width.  Otherwise, cursor can't
 30706              be displayed correctly.  */
 30707           glyph->pixel_width = 1;
 30708           glyph->padding_p = true;
 30709         }
 30710       glyph->ascent = it->ascent;
 30711       glyph->descent = it->descent;
 30712       glyph->voffset = it->voffset;
 30713       glyph->type = CHAR_GLYPH;
 30714       glyph->avoid_cursor_p = it->avoid_cursor_p;
 30715       glyph->multibyte_p = it->multibyte_p;
 30716       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30717         {
 30718           /* In R2L rows, the left and the right box edges need to be
 30719              drawn in reverse direction.  */
 30720           glyph->right_box_line_p = it->start_of_box_run_p;
 30721           glyph->left_box_line_p = it->end_of_box_run_p;
 30722         }
 30723       else
 30724         {
 30725           glyph->left_box_line_p = it->start_of_box_run_p;
 30726           glyph->right_box_line_p = it->end_of_box_run_p;
 30727         }
 30728       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 30729                                       || it->phys_descent > it->descent);
 30730       glyph->glyph_not_available_p = it->glyph_not_available_p;
 30731       glyph->face_id = it->face_id;
 30732       glyph->u.ch = it->char_to_display;
 30733       glyph->slice.img = null_glyph_slice;
 30734       glyph->font_type = FONT_TYPE_UNKNOWN;
 30735       if (it->bidi_p)
 30736         {
 30737           glyph->resolved_level = it->bidi_it.resolved_level;
 30738           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 30739           glyph->bidi_type = it->bidi_it.type;
 30740         }
 30741       else
 30742         {
 30743           glyph->resolved_level = 0;
 30744           glyph->bidi_type = UNKNOWN_BT;
 30745         }
 30746       ++it->glyph_row->used[area];
 30747     }
 30748   else
 30749     IT_EXPAND_MATRIX_WIDTH (it, area);
 30750 }
 30751 
 30752 /* Store one glyph for the composition IT->cmp_it.id in IT->glyph_row.
 30753    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30754 
 30755 static void
 30756 append_composite_glyph (struct it *it)
 30757 {
 30758   struct glyph *glyph;
 30759   enum glyph_row_area area = it->area;
 30760 
 30761   eassert (it->glyph_row);
 30762 
 30763   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30764   if (glyph < it->glyph_row->glyphs[area + 1])
 30765     {
 30766       /* If the glyph row is reversed, we need to prepend the glyph
 30767          rather than append it.  */
 30768       if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
 30769         {
 30770           struct glyph *g;
 30771 
 30772           /* Make room for the new glyph.  */
 30773           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 30774             g[1] = *g;
 30775           glyph = it->glyph_row->glyphs[it->area];
 30776         }
 30777       glyph->charpos = it->cmp_it.charpos;
 30778       glyph->object = it->object;
 30779       eassert (it->pixel_width <= SHRT_MAX);
 30780       glyph->pixel_width = it->pixel_width;
 30781       glyph->ascent = it->ascent;
 30782       glyph->descent = it->descent;
 30783       glyph->voffset = it->voffset;
 30784       glyph->type = COMPOSITE_GLYPH;
 30785       if (it->cmp_it.ch < 0)
 30786         {
 30787           glyph->u.cmp.automatic = false;
 30788           glyph->u.cmp.id = it->cmp_it.id;
 30789           glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
 30790         }
 30791       else
 30792         {
 30793           glyph->u.cmp.automatic = true;
 30794           glyph->u.cmp.id = it->cmp_it.id;
 30795           glyph->slice.cmp.from = it->cmp_it.from;
 30796           glyph->slice.cmp.to = it->cmp_it.to - 1;
 30797         }
 30798       glyph->avoid_cursor_p = it->avoid_cursor_p;
 30799       glyph->multibyte_p = it->multibyte_p;
 30800       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30801         {
 30802           /* In R2L rows, the left and the right box edges need to be
 30803              drawn in reverse direction.  */
 30804           glyph->right_box_line_p = it->start_of_box_run_p;
 30805           glyph->left_box_line_p = it->end_of_box_run_p;
 30806         }
 30807       else
 30808         {
 30809           glyph->left_box_line_p = it->start_of_box_run_p;
 30810           glyph->right_box_line_p = it->end_of_box_run_p;
 30811         }
 30812       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 30813                                       || it->phys_descent > it->descent);
 30814       glyph->padding_p = false;
 30815       glyph->glyph_not_available_p = it->glyph_not_available_p;
 30816       glyph->face_id = it->face_id;
 30817       glyph->font_type = FONT_TYPE_UNKNOWN;
 30818       if (it->bidi_p)
 30819         {
 30820           glyph->resolved_level = it->bidi_it.resolved_level;
 30821           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 30822           glyph->bidi_type = it->bidi_it.type;
 30823         }
 30824       ++it->glyph_row->used[area];
 30825     }
 30826   else
 30827     IT_EXPAND_MATRIX_WIDTH (it, area);
 30828 }
 30829 
 30830 
 30831 /* Change IT->ascent and IT->height according to the setting of
 30832    IT->voffset.  */
 30833 
 30834 static void
 30835 take_vertical_position_into_account (struct it *it)
 30836 {
 30837   if (it->voffset)
 30838     {
 30839       if (it->voffset < 0)
 30840         /* Increase the ascent so that we can display the text higher
 30841            in the line.  */
 30842         it->ascent -= it->voffset;
 30843       else
 30844         /* Increase the descent so that we can display the text lower
 30845            in the line.  */
 30846         it->descent += it->voffset;
 30847     }
 30848 }
 30849 
 30850 
 30851 /* Produce glyphs/get display metrics for the image IT is loaded with.
 30852    See the description of struct display_iterator in dispextern.h for
 30853    an overview of struct display_iterator.  */
 30854 
 30855 static void
 30856 produce_image_glyph (struct it *it)
 30857 {
 30858   struct image *img;
 30859   struct face *face;
 30860   int glyph_ascent, crop;
 30861   struct glyph_slice slice;
 30862 
 30863   eassert (it->what == IT_IMAGE);
 30864 
 30865   face = FACE_FROM_ID (it->f, it->face_id);
 30866   /* Make sure X resources of the face is loaded.  */
 30867   prepare_face_for_display (it->f, face);
 30868 
 30869   if (it->image_id < 0)
 30870     {
 30871       /* Fringe bitmap.  */
 30872       it->ascent = it->phys_ascent = 0;
 30873       it->descent = it->phys_descent = 0;
 30874       it->pixel_width = 0;
 30875       it->nglyphs = 0;
 30876       return;
 30877     }
 30878 
 30879   img = IMAGE_FROM_ID (it->f, it->image_id);
 30880   /* Make sure X resources of the image is loaded.  */
 30881   prepare_image_for_display (it->f, img);
 30882 
 30883   slice.x = slice.y = 0;
 30884   slice.width = img->width;
 30885   slice.height = img->height;
 30886 
 30887   if (FIXNUMP (it->slice.x))
 30888     slice.x = XFIXNUM (it->slice.x);
 30889   else if (FLOATP (it->slice.x))
 30890     slice.x = XFLOAT_DATA (it->slice.x) * img->width;
 30891 
 30892   if (FIXNUMP (it->slice.y))
 30893     slice.y = XFIXNUM (it->slice.y);
 30894   else if (FLOATP (it->slice.y))
 30895     slice.y = XFLOAT_DATA (it->slice.y) * img->height;
 30896 
 30897   if (FIXNUMP (it->slice.width))
 30898     slice.width = XFIXNUM (it->slice.width);
 30899   else if (FLOATP (it->slice.width))
 30900     slice.width = XFLOAT_DATA (it->slice.width) * img->width;
 30901 
 30902   if (FIXNUMP (it->slice.height))
 30903     slice.height = XFIXNUM (it->slice.height);
 30904   else if (FLOATP (it->slice.height))
 30905     slice.height = XFLOAT_DATA (it->slice.height) * img->height;
 30906 
 30907   if (slice.x >= img->width)
 30908     slice.x = img->width;
 30909   if (slice.y >= img->height)
 30910     slice.y = img->height;
 30911   if (slice.x + slice.width >= img->width)
 30912     slice.width = img->width - slice.x;
 30913   if (slice.y + slice.height > img->height)
 30914     slice.height = img->height - slice.y;
 30915 
 30916   if (slice.width == 0 || slice.height == 0)
 30917     return;
 30918 
 30919   it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
 30920 
 30921   it->descent = slice.height - glyph_ascent;
 30922   if (slice.y == 0)
 30923     it->descent += img->vmargin;
 30924   if (slice.y + slice.height == img->height)
 30925     it->descent += img->vmargin;
 30926   it->phys_descent = it->descent;
 30927 
 30928   it->pixel_width = slice.width;
 30929   if (slice.x == 0)
 30930     it->pixel_width += img->hmargin;
 30931   if (slice.x + slice.width == img->width)
 30932     it->pixel_width += img->hmargin;
 30933 
 30934   /* It's quite possible for images to have an ascent greater than
 30935      their height, so don't get confused in that case.  */
 30936   if (it->descent < 0)
 30937     it->descent = 0;
 30938 
 30939   it->nglyphs = 1;
 30940 
 30941   if (face->box != FACE_NO_BOX)
 30942     {
 30943       /* If you change the logic here, please change it in
 30944          get_cursor_offset_for_mouse_face as well. */
 30945       if (face->box_horizontal_line_width > 0)
 30946         {
 30947           if (slice.y == 0)
 30948             it->ascent += face->box_horizontal_line_width;
 30949           if (slice.y + slice.height == img->height)
 30950             it->descent += face->box_horizontal_line_width;
 30951         }
 30952 
 30953       if (face->box_vertical_line_width > 0)
 30954         {
 30955           if (it->start_of_box_run_p && slice.x == 0)
 30956             it->pixel_width += face->box_vertical_line_width;
 30957           if (it->end_of_box_run_p && slice.x + slice.width == img->width)
 30958             it->pixel_width += face->box_vertical_line_width;
 30959         }
 30960     }
 30961 
 30962   take_vertical_position_into_account (it);
 30963 
 30964   /* Automatically crop wide image glyphs at right edge so we can
 30965      draw the cursor on same display row.  */
 30966   if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
 30967       && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 30968     {
 30969       it->pixel_width -= crop;
 30970       slice.width -= crop;
 30971     }
 30972 
 30973   if (it->glyph_row)
 30974     {
 30975       struct glyph *glyph;
 30976       enum glyph_row_area area = it->area;
 30977 
 30978       glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30979       if (it->glyph_row->reversed_p)
 30980         {
 30981           struct glyph *g;
 30982 
 30983           /* Make room for the new glyph.  */
 30984           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 30985             g[1] = *g;
 30986           glyph = it->glyph_row->glyphs[it->area];
 30987         }
 30988       if (glyph < it->glyph_row->glyphs[area + 1])
 30989         {
 30990           glyph->charpos = CHARPOS (it->position);
 30991           glyph->object = it->object;
 30992           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 30993           glyph->ascent = glyph_ascent;
 30994           glyph->descent = it->descent;
 30995           glyph->voffset = it->voffset;
 30996           glyph->type = IMAGE_GLYPH;
 30997           glyph->avoid_cursor_p = it->avoid_cursor_p;
 30998           glyph->multibyte_p = it->multibyte_p;
 30999           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31000             {
 31001               /* In R2L rows, the left and the right box edges need to be
 31002                  drawn in reverse direction.  */
 31003               glyph->right_box_line_p = it->start_of_box_run_p;
 31004               glyph->left_box_line_p = it->end_of_box_run_p;
 31005             }
 31006           else
 31007             {
 31008               glyph->left_box_line_p = it->start_of_box_run_p;
 31009               glyph->right_box_line_p = it->end_of_box_run_p;
 31010             }
 31011           glyph->overlaps_vertically_p = false;
 31012           glyph->padding_p = false;
 31013           glyph->glyph_not_available_p = false;
 31014           glyph->face_id = it->face_id;
 31015           glyph->u.img_id = img->id;
 31016           glyph->slice.img = slice;
 31017           glyph->font_type = FONT_TYPE_UNKNOWN;
 31018           if (it->bidi_p)
 31019             {
 31020               glyph->resolved_level = it->bidi_it.resolved_level;
 31021               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31022               glyph->bidi_type = it->bidi_it.type;
 31023             }
 31024           ++it->glyph_row->used[area];
 31025         }
 31026       else
 31027         IT_EXPAND_MATRIX_WIDTH (it, area);
 31028     }
 31029 }
 31030 
 31031 static void
 31032 produce_xwidget_glyph (struct it *it)
 31033 {
 31034 #ifdef HAVE_XWIDGETS
 31035   struct xwidget *xw;
 31036   int glyph_ascent, crop;
 31037   eassert (it->what == IT_XWIDGET);
 31038 
 31039   struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31040   /* Make sure X resources of the face is loaded.  */
 31041   prepare_face_for_display (it->f, face);
 31042 
 31043   xw = it->xwidget;
 31044   it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
 31045   it->descent = xw->height/2;
 31046   it->phys_descent = it->descent;
 31047   it->pixel_width = xw->width;
 31048   /* It's quite possible for images to have an ascent greater than
 31049      their height, so don't get confused in that case.  */
 31050   if (it->descent < 0)
 31051     it->descent = 0;
 31052 
 31053   it->nglyphs = 1;
 31054 
 31055   if (face->box != FACE_NO_BOX)
 31056     {
 31057       if (face->box_horizontal_line_width > 0)
 31058         {
 31059           it->ascent += face->box_horizontal_line_width;
 31060           it->descent += face->box_horizontal_line_width;
 31061         }
 31062 
 31063       if (face->box_vertical_line_width > 0)
 31064         {
 31065           if (it->start_of_box_run_p)
 31066             it->pixel_width += face->box_vertical_line_width;
 31067           it->pixel_width += face->box_vertical_line_width;
 31068         }
 31069     }
 31070 
 31071   take_vertical_position_into_account (it);
 31072 
 31073   /* Automatically crop wide image glyphs at right edge so we can
 31074      draw the cursor on same display row.  */
 31075   crop = it->pixel_width - (it->last_visible_x - it->current_x);
 31076   if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31077     it->pixel_width -= crop;
 31078 
 31079   if (it->glyph_row)
 31080     {
 31081       enum glyph_row_area area = it->area;
 31082       struct glyph *glyph
 31083         = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31084 
 31085       if (it->glyph_row->reversed_p)
 31086         {
 31087           struct glyph *g;
 31088 
 31089           /* Make room for the new glyph.  */
 31090           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31091             g[1] = *g;
 31092           glyph = it->glyph_row->glyphs[it->area];
 31093         }
 31094       if (glyph < it->glyph_row->glyphs[area + 1])
 31095         {
 31096           glyph->charpos = CHARPOS (it->position);
 31097           glyph->object = it->object;
 31098           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31099           glyph->ascent = glyph_ascent;
 31100           glyph->descent = it->descent;
 31101           glyph->voffset = it->voffset;
 31102           glyph->type = XWIDGET_GLYPH;
 31103           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31104           glyph->multibyte_p = it->multibyte_p;
 31105           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31106             {
 31107               /* In R2L rows, the left and the right box edges need to be
 31108                  drawn in reverse direction.  */
 31109               glyph->right_box_line_p = it->start_of_box_run_p;
 31110               glyph->left_box_line_p = it->end_of_box_run_p;
 31111             }
 31112           else
 31113             {
 31114               glyph->left_box_line_p = it->start_of_box_run_p;
 31115               glyph->right_box_line_p = it->end_of_box_run_p;
 31116             }
 31117           glyph->overlaps_vertically_p = 0;
 31118           glyph->padding_p = 0;
 31119           glyph->glyph_not_available_p = 0;
 31120           glyph->face_id = it->face_id;
 31121           glyph->u.xwidget = it->xwidget->xwidget_id;
 31122           glyph->font_type = FONT_TYPE_UNKNOWN;
 31123           if (it->bidi_p)
 31124             {
 31125               glyph->resolved_level = it->bidi_it.resolved_level;
 31126               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31127               glyph->bidi_type = it->bidi_it.type;
 31128             }
 31129           ++it->glyph_row->used[area];
 31130         }
 31131       else
 31132         IT_EXPAND_MATRIX_WIDTH (it, area);
 31133     }
 31134 #endif
 31135 }
 31136 
 31137 /* Append a stretch glyph to IT->glyph_row.  OBJECT is the source
 31138    of the glyph, WIDTH and HEIGHT are the width and height of the
 31139    stretch.  ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT).  */
 31140 
 31141 static void
 31142 append_stretch_glyph (struct it *it, Lisp_Object object,
 31143                       int width, int height, int ascent)
 31144 {
 31145   struct glyph *glyph;
 31146   enum glyph_row_area area = it->area;
 31147 
 31148   eassert (ascent >= 0 && ascent <= height);
 31149 
 31150   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31151   if (glyph < it->glyph_row->glyphs[area + 1])
 31152     {
 31153       /* If the glyph row is reversed, we need to prepend the glyph
 31154          rather than append it.  */
 31155       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31156         {
 31157           struct glyph *g;
 31158 
 31159           /* Make room for the additional glyph.  */
 31160           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31161             g[1] = *g;
 31162           glyph = it->glyph_row->glyphs[area];
 31163 
 31164           /* Decrease the width of the first glyph of the row that
 31165              begins before first_visible_x (e.g., due to hscroll).
 31166              This is so the overall width of the row becomes smaller
 31167              by the scroll amount, and the stretch glyph appended by
 31168              extend_face_to_end_of_line will be wider, to shift the
 31169              row glyphs to the right.  (In L2R rows, the corresponding
 31170              left-shift effect is accomplished by setting row->x to a
 31171              negative value, which won't work with R2L rows.)
 31172 
 31173              This must leave us with a positive value of WIDTH, since
 31174              otherwise the call to move_it_in_display_line_to at the
 31175              beginning of display_line would have got past the entire
 31176              first glyph, and then it->current_x would have been
 31177              greater or equal to it->first_visible_x.  */
 31178           if (it->current_x < it->first_visible_x)
 31179             width -= it->first_visible_x - it->current_x;
 31180           eassert (width > 0);
 31181         }
 31182       glyph->charpos = CHARPOS (it->position);
 31183       glyph->object = object;
 31184       /* FIXME: It would be better to use TYPE_MAX here, but
 31185          __typeof__ is not portable enough...  */
 31186       glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
 31187       glyph->ascent = ascent;
 31188       glyph->descent = height - ascent;
 31189       glyph->voffset = it->voffset;
 31190       glyph->type = STRETCH_GLYPH;
 31191       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31192       glyph->multibyte_p = it->multibyte_p;
 31193       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31194         {
 31195           /* In R2L rows, the left and the right box edges need to be
 31196              drawn in reverse direction.  */
 31197           glyph->right_box_line_p = it->start_of_box_run_p;
 31198           glyph->left_box_line_p = it->end_of_box_run_p;
 31199         }
 31200       else
 31201         {
 31202           glyph->left_box_line_p = it->start_of_box_run_p;
 31203           glyph->right_box_line_p = it->end_of_box_run_p;
 31204         }
 31205       glyph->overlaps_vertically_p = false;
 31206       glyph->padding_p = false;
 31207       glyph->glyph_not_available_p = false;
 31208       glyph->face_id = it->face_id;
 31209       glyph->u.stretch.ascent = ascent;
 31210       glyph->u.stretch.height = height;
 31211       glyph->slice.img = null_glyph_slice;
 31212       glyph->font_type = FONT_TYPE_UNKNOWN;
 31213       if (it->bidi_p)
 31214         {
 31215           glyph->resolved_level = it->bidi_it.resolved_level;
 31216           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31217           glyph->bidi_type = it->bidi_it.type;
 31218         }
 31219       else
 31220         {
 31221           glyph->resolved_level = 0;
 31222           glyph->bidi_type = UNKNOWN_BT;
 31223         }
 31224       ++it->glyph_row->used[area];
 31225     }
 31226   else
 31227     IT_EXPAND_MATRIX_WIDTH (it, area);
 31228 }
 31229 
 31230 #endif  /* HAVE_WINDOW_SYSTEM */
 31231 
 31232 /* Produce a stretch glyph for iterator IT.  IT->object is the value
 31233    of the display property.  The value must be a list of the form
 31234    `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
 31235    being recognized:
 31236 
 31237    1. `:width WIDTH' specifies that the space should be WIDTH *
 31238    canonical char width wide.  WIDTH may be an integer or floating
 31239    point number.
 31240 
 31241    2. `:relative-width FACTOR' specifies that the width of the stretch
 31242    should be computed from the width of the first character having the
 31243    `display' property, and should be FACTOR times that width.
 31244 
 31245    3. `:align-to HPOS' specifies that the space should be wide enough
 31246    to reach HPOS, a value in canonical character units.
 31247 
 31248    Exactly one of the above pairs must be present.
 31249 
 31250    4. `:height HEIGHT' specifies that the height of the stretch produced
 31251    should be HEIGHT, measured in canonical character units.
 31252 
 31253    5. `:relative-height FACTOR' specifies that the height of the
 31254    stretch should be FACTOR times the height of the characters having
 31255    the display property.
 31256 
 31257    Either none or exactly one of 4 or 5 must be present.
 31258 
 31259    6. `:ascent ASCENT'  specifies that ASCENT percent of the height
 31260    of the stretch should be used for the ascent of the stretch.
 31261    ASCENT must be in the range 0 <= ASCENT <= 100.  */
 31262 
 31263 void
 31264 produce_stretch_glyph (struct it *it)
 31265 {
 31266   /* (space :width WIDTH :height HEIGHT ...)  */
 31267   Lisp_Object prop, plist;
 31268   int width = 0, height = 0, align_to = -1;
 31269   bool zero_width_ok_p = false;
 31270   double tem;
 31271   struct font *font = NULL;
 31272 
 31273 #ifdef HAVE_WINDOW_SYSTEM
 31274   int ascent = 0;
 31275   bool zero_height_ok_p = false;
 31276   struct face *face = NULL;     /* shut up GCC's -Wmaybe-uninitialized */
 31277 
 31278   if (FRAME_WINDOW_P (it->f))
 31279     {
 31280       face = FACE_FROM_ID (it->f, it->face_id);
 31281       font = face->font ? face->font : FRAME_FONT (it->f);
 31282       prepare_face_for_display (it->f, face);
 31283     }
 31284 #endif
 31285 
 31286   /* List should start with `space'.  */
 31287   eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
 31288   plist = XCDR (it->object);
 31289 
 31290   /* Compute the width of the stretch.  */
 31291   if ((prop = plist_get (plist, QCwidth), !NILP (prop))
 31292       && calc_pixel_width_or_height (&tem, it, prop, font, true, NULL))
 31293     {
 31294       /* Absolute width `:width WIDTH' specified and valid.  */
 31295       zero_width_ok_p = true;
 31296       width = (int)tem;
 31297     }
 31298   else if (prop = plist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
 31299     {
 31300       /* Relative width `:relative-width FACTOR' specified and valid.
 31301          Compute the width of the characters having this `display'
 31302          property.  */
 31303       struct it it2;
 31304       Lisp_Object object =
 31305         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31306       unsigned char *p = (STRINGP (object)
 31307                           ? SDATA (object) + IT_STRING_BYTEPOS (*it)
 31308                           : BYTE_POS_ADDR (IT_BYTEPOS (*it)));
 31309       bool multibyte_p =
 31310         STRINGP (object) ? STRING_MULTIBYTE (object) : it->multibyte_p;
 31311 
 31312       it2 = *it;
 31313       if (multibyte_p)
 31314         {
 31315           it2.c = it2.char_to_display = string_char_and_length (p, &it2.len);
 31316 #ifdef HAVE_WINDOW_SYSTEM
 31317           if (FRAME_WINDOW_P (it->f) && ! ASCII_CHAR_P (it2.c))
 31318             it2.face_id = FACE_FOR_CHAR (it->f, face, it2.c,
 31319                                          IT_CHARPOS (*it),
 31320                                          STRINGP (object)? object : Qnil);
 31321 #endif
 31322         }
 31323       else
 31324         {
 31325           it2.c = it2.char_to_display = *p, it2.len = 1;
 31326           if (! ASCII_CHAR_P (it2.c))
 31327             it2.char_to_display = BYTE8_TO_CHAR (it2.c);
 31328         }
 31329 
 31330       it2.glyph_row = NULL;
 31331       it2.what = IT_CHARACTER;
 31332       PRODUCE_GLYPHS (&it2);
 31333       width = NUMVAL (prop) * it2.pixel_width;
 31334     }
 31335   else if ((prop = plist_get (plist, QCalign_to), !NILP (prop))
 31336            && calc_pixel_width_or_height (&tem, it, prop, font, true,
 31337                                           &align_to))
 31338     {
 31339       int x = it->current_x + it->continuation_lines_width;
 31340       int x0 = x;
 31341       /* Adjust for line numbers, if needed.   */
 31342       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31343         {
 31344           x -= it->lnum_pixel_width;
 31345           /* Restore the original width, if required.  */
 31346           if (x + it->stretch_adjust >= it->first_visible_x)
 31347             x += it->stretch_adjust;
 31348         }
 31349 
 31350       if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
 31351         align_to = (align_to < 0
 31352                     ? 0
 31353                     : align_to - window_box_left_offset (it->w, TEXT_AREA));
 31354       else if (align_to < 0)
 31355         align_to = window_box_left_offset (it->w, TEXT_AREA);
 31356       width = max (0, (int)tem + align_to - x);
 31357 
 31358       int next_x = x + width;
 31359       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31360         {
 31361           /* If the line is hscrolled, and the stretch starts before
 31362              the first visible pixel, simulate negative row->x.  */
 31363           if (x < it->first_visible_x)
 31364             {
 31365               next_x -= it->first_visible_x - x;
 31366               it->stretch_adjust = it->first_visible_x - x;
 31367             }
 31368           else
 31369             next_x -= it->stretch_adjust;
 31370         }
 31371       width = next_x - x0;
 31372       zero_width_ok_p = true;
 31373     }
 31374   else
 31375     /* Nothing specified -> width defaults to canonical char width.  */
 31376     width = FRAME_COLUMN_WIDTH (it->f);
 31377 
 31378   if (width <= 0 && (width < 0 || !zero_width_ok_p))
 31379     width = 1;
 31380 
 31381 #ifdef HAVE_WINDOW_SYSTEM
 31382   /* Compute height.  */
 31383   if (FRAME_WINDOW_P (it->f))
 31384     {
 31385       int default_height = normal_char_height (font, ' ');
 31386 
 31387       if ((prop = plist_get (plist, QCheight), !NILP (prop))
 31388           && calc_pixel_width_or_height (&tem, it, prop, font, false, NULL))
 31389         {
 31390           height = (int)tem;
 31391           zero_height_ok_p = true;
 31392         }
 31393       else if (prop = plist_get (plist, QCrelative_height),
 31394                NUMVAL (prop) > 0)
 31395         height = default_height * NUMVAL (prop);
 31396       else
 31397         height = default_height;
 31398 
 31399       if (height <= 0 && (height < 0 || !zero_height_ok_p))
 31400         height = 1;
 31401 
 31402       /* Compute percentage of height used for ascent.  If
 31403          `:ascent ASCENT' is present and valid, use that.  Otherwise,
 31404          derive the ascent from the font in use.  */
 31405       if (prop = plist_get (plist, QCascent),
 31406           NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
 31407         ascent = height * NUMVAL (prop) / 100.0;
 31408       else if (!NILP (prop)
 31409                && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
 31410         ascent = min (max (0, (int)tem), height);
 31411       else
 31412         ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
 31413     }
 31414   else
 31415 #endif  /* HAVE_WINDOW_SYSTEM */
 31416     height = 1;
 31417 
 31418   if (width > 0
 31419       && it->area == TEXT_AREA && it->line_wrap != TRUNCATE
 31420       && it->current_x + width > it->last_visible_x)
 31421     {
 31422       width = it->last_visible_x - it->current_x;
 31423 #ifdef HAVE_WINDOW_SYSTEM
 31424       /* Subtract one more pixel from the stretch width, but only on
 31425          GUI frames, since on a TTY each glyph is one "pixel" wide.  */
 31426       width -= FRAME_WINDOW_P (it->f);
 31427 #endif
 31428     }
 31429 
 31430   if (width > 0 && height > 0 && it->glyph_row)
 31431     {
 31432       Lisp_Object o_object = it->object;
 31433       Lisp_Object object =
 31434         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31435       int n = width;
 31436 
 31437       if (!STRINGP (object))
 31438         object = it->w->contents;
 31439 #ifdef HAVE_WINDOW_SYSTEM
 31440       if (FRAME_WINDOW_P (it->f))
 31441         append_stretch_glyph (it, object, width, height, ascent);
 31442       else
 31443 #endif
 31444         {
 31445           it->object = object;
 31446           it->char_to_display = ' ';
 31447           it->pixel_width = it->len = 1;
 31448           while (n--)
 31449             tty_append_glyph (it);
 31450           it->object = o_object;
 31451         }
 31452     }
 31453 
 31454   it->pixel_width = width;
 31455 #ifdef HAVE_WINDOW_SYSTEM
 31456   if (FRAME_WINDOW_P (it->f))
 31457     {
 31458       it->ascent = it->phys_ascent = ascent;
 31459       it->descent = it->phys_descent = height - it->ascent;
 31460       it->nglyphs = width > 0 && height > 0;
 31461       take_vertical_position_into_account (it);
 31462     }
 31463   else
 31464 #endif
 31465     it->nglyphs = width;
 31466 }
 31467 
 31468 /* Get information about special display element WHAT in an
 31469    environment described by IT.  WHAT is one of IT_TRUNCATION or
 31470    IT_CONTINUATION.  Maybe produce glyphs for WHAT if IT has a
 31471    non-null glyph_row member.  This function ensures that fields like
 31472    face_id, c, len of IT are left untouched.  */
 31473 
 31474 static void
 31475 produce_special_glyphs (struct it *it, enum display_element_type what)
 31476 {
 31477   struct it temp_it;
 31478   Lisp_Object gc;
 31479   GLYPH glyph;
 31480 
 31481   temp_it = *it;
 31482   temp_it.object = Qnil;
 31483   memset (&temp_it.current, 0, sizeof temp_it.current);
 31484 
 31485   if (what == IT_CONTINUATION)
 31486     {
 31487       /* Continuation glyph.  For R2L lines, we mirror it by hand.  */
 31488       if (it->bidi_it.paragraph_dir == R2L)
 31489         SET_GLYPH_FROM_CHAR (glyph, '/');
 31490       else
 31491         SET_GLYPH_FROM_CHAR (glyph, '\\');
 31492       if (it->dp
 31493           && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31494         {
 31495           /* FIXME: Should we mirror GC for R2L lines?  */
 31496           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31497           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31498         }
 31499     }
 31500   else if (what == IT_TRUNCATION)
 31501     {
 31502       /* Truncation glyph.  */
 31503       SET_GLYPH_FROM_CHAR (glyph, '$');
 31504       if (it->dp
 31505           && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31506         {
 31507           /* FIXME: Should we mirror GC for R2L lines?  */
 31508           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31509           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31510         }
 31511     }
 31512   else
 31513     emacs_abort ();
 31514 
 31515 #ifdef HAVE_WINDOW_SYSTEM
 31516   /* On a GUI frame, when the right fringe (left fringe for R2L rows)
 31517      is turned off, we precede the truncation/continuation glyphs by a
 31518      stretch glyph whose width is computed such that these special
 31519      glyphs are aligned at the window margin, even when very different
 31520      fonts are used in different glyph rows.  */
 31521   if (FRAME_WINDOW_P (temp_it.f)
 31522       /* init_iterator calls this with it->glyph_row == NULL, and it
 31523          wants only the pixel width of the truncation/continuation
 31524          glyphs.  */
 31525       && temp_it.glyph_row
 31526       /* insert_left_trunc_glyphs calls us at the beginning of the
 31527          row, and it has its own calculation of the stretch glyph
 31528          width.  */
 31529       && temp_it.glyph_row->used[TEXT_AREA] > 0
 31530       && (temp_it.glyph_row->reversed_p
 31531           ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
 31532           : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
 31533     {
 31534       int stretch_width = temp_it.last_visible_x - temp_it.current_x;
 31535 
 31536       if (stretch_width > 0)
 31537         {
 31538           struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
 31539           struct font *font =
 31540             face->font ? face->font : FRAME_FONT (temp_it.f);
 31541           int stretch_ascent =
 31542             (((temp_it.ascent + temp_it.descent)
 31543               * FONT_BASE (font)) / FONT_HEIGHT (font));
 31544 
 31545           append_stretch_glyph (&temp_it, Qnil, stretch_width,
 31546                                 temp_it.ascent + temp_it.descent,
 31547                                 stretch_ascent);
 31548         }
 31549     }
 31550 #endif
 31551 
 31552   temp_it.dp = NULL;
 31553   temp_it.what = IT_CHARACTER;
 31554   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31555   temp_it.face_id = GLYPH_FACE (glyph);
 31556   temp_it.len = CHAR_BYTES (temp_it.c);
 31557 
 31558   PRODUCE_GLYPHS (&temp_it);
 31559   it->pixel_width = temp_it.pixel_width;
 31560   it->nglyphs = temp_it.nglyphs;
 31561 }
 31562 
 31563 #ifdef HAVE_WINDOW_SYSTEM
 31564 
 31565 /* Calculate line-height and line-spacing properties.
 31566    An integer value specifies explicit pixel value.
 31567    A float value specifies relative value to current face height.
 31568    A cons (float . face-name) specifies relative value to
 31569    height of specified face font.
 31570 
 31571    Returns height in pixels, or nil.  */
 31572 
 31573 static Lisp_Object
 31574 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
 31575                            int boff, bool override)
 31576 {
 31577   Lisp_Object face_name = Qnil;
 31578   int ascent, descent, height;
 31579 
 31580   if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
 31581     return val;
 31582 
 31583   if (CONSP (val))
 31584     {
 31585       face_name = XCAR (val);
 31586       val = XCDR (val);
 31587       if (!NUMBERP (val))
 31588         val = make_fixnum (1);
 31589       if (NILP (face_name))
 31590         {
 31591           height = it->ascent + it->descent;
 31592           goto scale;
 31593         }
 31594     }
 31595 
 31596   if (NILP (face_name))
 31597     {
 31598       font = FRAME_FONT (it->f);
 31599       boff = FRAME_BASELINE_OFFSET (it->f);
 31600     }
 31601   else if (EQ (face_name, Qt))
 31602     {
 31603       override = false;
 31604     }
 31605   else
 31606     {
 31607       int face_id;
 31608       struct face *face;
 31609 
 31610       face_id = lookup_named_face (it->w, it->f, face_name, false);
 31611       face = FACE_FROM_ID_OR_NULL (it->f, face_id);
 31612       if (face == NULL || ((font = face->font) == NULL))
 31613         return make_fixnum (-1);
 31614       boff = font->baseline_offset;
 31615       if (font->vertical_centering)
 31616         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31617     }
 31618 
 31619   normal_char_ascent_descent (font, -1, &ascent, &descent);
 31620 
 31621   if (override)
 31622     {
 31623       it->override_ascent = ascent;
 31624       it->override_descent = descent;
 31625       it->override_boff = boff;
 31626     }
 31627 
 31628   height = ascent + descent;
 31629 
 31630  scale:
 31631   /* FIXME: Check for overflow in multiplication or conversion.  */
 31632   if (FLOATP (val))
 31633     height = (int)(XFLOAT_DATA (val) * height);
 31634   else if (INTEGERP (val))
 31635     {
 31636       intmax_t v;
 31637       if (integer_to_intmax (val, &v))
 31638         height *= v;
 31639     }
 31640 
 31641   return make_fixnum (height);
 31642 }
 31643 
 31644 
 31645 /* Append a glyph for a glyphless character to IT->glyph_row.  FACE_ID
 31646    is a face ID to be used for the glyph.  FOR_NO_FONT is true if
 31647    and only if this is for a character for which no font was found.
 31648 
 31649    If the display method (it->glyphless_method) is
 31650    GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
 31651    length of the acronym or the hexadecimal string, UPPER_XOFF and
 31652    UPPER_YOFF are pixel offsets for the upper part of the string,
 31653    LOWER_XOFF and LOWER_YOFF are for the lower part.
 31654 
 31655    For the other display methods, LEN through LOWER_YOFF are zero.  */
 31656 
 31657 static void
 31658 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
 31659                         short upper_xoff, short upper_yoff,
 31660                         short lower_xoff, short lower_yoff)
 31661 {
 31662   struct glyph *glyph;
 31663   enum glyph_row_area area = it->area;
 31664 
 31665   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31666   if (glyph < it->glyph_row->glyphs[area + 1])
 31667     {
 31668       /* If the glyph row is reversed, we need to prepend the glyph
 31669          rather than append it.  */
 31670       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31671         {
 31672           struct glyph *g;
 31673 
 31674           /* Make room for the additional glyph.  */
 31675           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31676             g[1] = *g;
 31677           glyph = it->glyph_row->glyphs[area];
 31678         }
 31679       glyph->charpos = CHARPOS (it->position);
 31680       glyph->object = it->object;
 31681       eassert (it->pixel_width <= SHRT_MAX);
 31682       glyph->pixel_width = it->pixel_width;
 31683       glyph->ascent = it->ascent;
 31684       glyph->descent = it->descent;
 31685       glyph->voffset = it->voffset;
 31686       glyph->type = GLYPHLESS_GLYPH;
 31687       glyph->u.glyphless.method = it->glyphless_method;
 31688       glyph->u.glyphless.for_no_font = for_no_font;
 31689       glyph->u.glyphless.len = len;
 31690       glyph->u.glyphless.ch = it->c;
 31691       glyph->slice.glyphless.upper_xoff = upper_xoff;
 31692       glyph->slice.glyphless.upper_yoff = upper_yoff;
 31693       glyph->slice.glyphless.lower_xoff = lower_xoff;
 31694       glyph->slice.glyphless.lower_yoff = lower_yoff;
 31695       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31696       glyph->multibyte_p = it->multibyte_p;
 31697       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31698         {
 31699           /* In R2L rows, the left and the right box edges need to be
 31700              drawn in reverse direction.  */
 31701           glyph->right_box_line_p = it->start_of_box_run_p;
 31702           glyph->left_box_line_p = it->end_of_box_run_p;
 31703         }
 31704       else
 31705         {
 31706           glyph->left_box_line_p = it->start_of_box_run_p;
 31707           glyph->right_box_line_p = it->end_of_box_run_p;
 31708         }
 31709       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31710                                       || it->phys_descent > it->descent);
 31711       glyph->padding_p = false;
 31712       glyph->glyph_not_available_p = false;
 31713       glyph->face_id = face_id;
 31714       glyph->font_type = FONT_TYPE_UNKNOWN;
 31715       if (it->bidi_p)
 31716         {
 31717           glyph->resolved_level = it->bidi_it.resolved_level;
 31718           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31719           glyph->bidi_type = it->bidi_it.type;
 31720         }
 31721       ++it->glyph_row->used[area];
 31722     }
 31723   else
 31724     IT_EXPAND_MATRIX_WIDTH (it, area);
 31725 }
 31726 
 31727 
 31728 /* Produce a glyph for a glyphless character for iterator IT.
 31729    IT->glyphless_method specifies which method to use for displaying
 31730    the character.  See the description of enum
 31731    glyphless_display_method in dispextern.h for the detail.
 31732 
 31733    FOR_NO_FONT is true if and only if this is for a character for
 31734    which no font was found.  ACRONYM, if non-nil, is an acronym string
 31735    for the character.  */
 31736 
 31737 static void
 31738 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
 31739 {
 31740   int face_id;
 31741   struct face *face;
 31742   struct font *font;
 31743   int base_width, base_height, width, height;
 31744   short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
 31745   int len;
 31746 
 31747   /* Get the metrics of the base font.  We always refer to the current
 31748      ASCII face.  */
 31749   face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
 31750   font = face->font ? face->font : FRAME_FONT (it->f);
 31751   normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 31752   it->ascent += font->baseline_offset;
 31753   it->descent -= font->baseline_offset;
 31754   base_height = it->ascent + it->descent;
 31755   base_width = font->average_width;
 31756 
 31757   face_id = merge_glyphless_glyph_face (it);
 31758 
 31759   if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
 31760     {
 31761       it->pixel_width = THIN_SPACE_WIDTH;
 31762       len = 0;
 31763       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 31764     }
 31765   else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
 31766     {
 31767       width = CHARACTER_WIDTH (it->c);
 31768       if (width == 0)
 31769         width = 1;
 31770       else if (width > 4)
 31771         width = 4;
 31772       it->pixel_width = base_width * width;
 31773       len = 0;
 31774       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 31775     }
 31776   else
 31777     {
 31778       char buf[7];
 31779       const char *str;
 31780       unsigned int code[6];
 31781       int upper_len;
 31782       int ascent, descent;
 31783       struct font_metrics metrics_upper, metrics_lower;
 31784 
 31785       face = FACE_FROM_ID (it->f, face_id);
 31786       font = face->font ? face->font : FRAME_FONT (it->f);
 31787       prepare_face_for_display (it->f, face);
 31788 
 31789       if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
 31790         {
 31791           if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
 31792             acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
 31793           if (CONSP (acronym))
 31794             acronym = XCAR (acronym);
 31795           str = STRINGP (acronym) ? SSDATA (acronym) : "";
 31796         }
 31797       else
 31798         {
 31799           eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
 31800           sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
 31801           str = buf;
 31802         }
 31803       for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
 31804         code[len] = font->driver->encode_char (font, str[len]);
 31805       upper_len = (len + 1) / 2;
 31806       font->driver->text_extents (font, code, upper_len,
 31807                                   &metrics_upper);
 31808       font->driver->text_extents (font, code + upper_len, len - upper_len,
 31809                                   &metrics_lower);
 31810 
 31811 
 31812 
 31813       /* +4 is for vertical bars of a box plus 1-pixel spaces at both side.  */
 31814       width = max (metrics_upper.width, metrics_lower.width) + 4;
 31815       upper_xoff = lower_xoff = 2; /* the typical case */
 31816       if (base_width >= width)
 31817         {
 31818           /* Align the upper to the left, the lower to the right.  */
 31819           it->pixel_width = base_width;
 31820           lower_xoff = base_width - 2 - metrics_lower.width;
 31821         }
 31822       else
 31823         {
 31824           /* Center the shorter one.  */
 31825           it->pixel_width = width;
 31826           if (metrics_upper.width >= metrics_lower.width)
 31827             lower_xoff = (width - metrics_lower.width) / 2;
 31828           else
 31829             upper_xoff = (width - metrics_upper.width) / 2;
 31830         }
 31831 
 31832       /* +5 is for horizontal bars of a box plus 1-pixel spaces at
 31833          top, bottom, and between upper and lower strings.  */
 31834       height = (metrics_upper.ascent + metrics_upper.descent
 31835                 + metrics_lower.ascent + metrics_lower.descent) + 5;
 31836       /* Center vertically.
 31837          H:base_height, D:base_descent
 31838          h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
 31839 
 31840          ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
 31841          descent = D - H/2 + h/2;
 31842          lower_yoff = descent - 2 - ld;
 31843          upper_yoff = lower_yoff - la - 1 - ud;  */
 31844       ascent = - (it->descent - (base_height + height + 1) / 2);
 31845       descent = it->descent - (base_height - height) / 2;
 31846       lower_yoff = descent - 2 - metrics_lower.descent;
 31847       upper_yoff = (lower_yoff - metrics_lower.ascent - 1
 31848                     - metrics_upper.descent);
 31849       /* Don't make the height shorter than the base height.  */
 31850       if (height > base_height)
 31851         {
 31852           it->ascent = ascent;
 31853           it->descent = descent;
 31854         }
 31855     }
 31856 
 31857   it->phys_ascent = it->ascent;
 31858   it->phys_descent = it->descent;
 31859   if (it->glyph_row)
 31860     append_glyphless_glyph (it, face_id, for_no_font, len,
 31861                             upper_xoff, upper_yoff,
 31862                             lower_xoff, lower_yoff);
 31863   it->nglyphs = 1;
 31864   take_vertical_position_into_account (it);
 31865 }
 31866 
 31867 
 31868 /* If face has a box, add the box thickness to the character
 31869    height.  If character has a box line to the left and/or
 31870    right, add the box line width to the character's width.  */
 31871 #define IT_APPLY_FACE_BOX(it, face)                             \
 31872   do {                                                          \
 31873     if (face->box != FACE_NO_BOX)                               \
 31874       {                                                         \
 31875         int thick = face->box_horizontal_line_width;            \
 31876         if (thick > 0)                                          \
 31877           {                                                     \
 31878             it->ascent += thick;                                \
 31879             it->descent += thick;                               \
 31880           }                                                     \
 31881                                                                 \
 31882         thick = face->box_vertical_line_width;                  \
 31883         if (thick > 0)                                          \
 31884           {                                                     \
 31885             if (it->start_of_box_run_p)                         \
 31886               it->pixel_width += thick;                         \
 31887             if (it->end_of_box_run_p)                           \
 31888               it->pixel_width += thick;                         \
 31889           }                                                     \
 31890       }                                                         \
 31891     } while (false)
 31892 
 31893 /* RIF:
 31894    Produce glyphs/get display metrics for the display element IT is
 31895    loaded with.  See the description of struct it in dispextern.h
 31896    for an overview of struct it.  */
 31897 
 31898 void
 31899 gui_produce_glyphs (struct it *it)
 31900 {
 31901   int extra_line_spacing = it->extra_line_spacing;
 31902 
 31903   it->glyph_not_available_p = false;
 31904 
 31905   if (it->what == IT_CHARACTER)
 31906     {
 31907       unsigned char2b;
 31908       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31909       struct font *font = face->font;
 31910       struct font_metrics *pcm = NULL;
 31911       int boff;                 /* Baseline offset.  */
 31912 
 31913       if (font == NULL)
 31914         {
 31915           /* When no suitable font is found, display this character by
 31916              the method specified in the first extra slot of
 31917              Vglyphless_char_display.  */
 31918           Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 31919 
 31920           eassert (it->what == IT_GLYPHLESS);
 31921           produce_glyphless_glyph (it, true,
 31922                                    STRINGP (acronym) ? acronym : Qnil);
 31923           goto done;
 31924         }
 31925 
 31926       boff = font->baseline_offset;
 31927       if (font->vertical_centering)
 31928         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31929 
 31930       if (it->char_to_display != '\n' && it->char_to_display != '\t')
 31931         {
 31932           it->nglyphs = 1;
 31933 
 31934           if (it->override_ascent >= 0)
 31935             {
 31936               it->ascent = it->override_ascent;
 31937               it->descent = it->override_descent;
 31938               boff = it->override_boff;
 31939             }
 31940           else
 31941             {
 31942               it->ascent = FONT_BASE (font) + boff;
 31943               it->descent = FONT_DESCENT (font) - boff;
 31944             }
 31945 
 31946           if (get_char_glyph_code (it->char_to_display, font, &char2b))
 31947             {
 31948               pcm = get_per_char_metric (font, &char2b);
 31949               if (pcm->width == 0
 31950                   && pcm->rbearing == 0 && pcm->lbearing == 0)
 31951                 pcm = NULL;
 31952             }
 31953 
 31954           if (pcm)
 31955             {
 31956               it->phys_ascent = pcm->ascent + boff;
 31957               it->phys_descent = pcm->descent - boff;
 31958               it->pixel_width = pcm->width;
 31959               /* Don't use font-global values for ascent and descent
 31960                  if they result in an exceedingly large line height.  */
 31961               if (it->override_ascent < 0)
 31962                 {
 31963                   if (FONT_TOO_HIGH (font))
 31964                     {
 31965                       it->ascent = it->phys_ascent;
 31966                       it->descent = it->phys_descent;
 31967                       /* These limitations are enforced by an
 31968                          assertion near the end of this function.  */
 31969                       if (it->ascent < 0)
 31970                         it->ascent = 0;
 31971                       if (it->descent < 0)
 31972                         it->descent = 0;
 31973                     }
 31974                 }
 31975             }
 31976           else
 31977             {
 31978               it->glyph_not_available_p = true;
 31979               it->phys_ascent = it->ascent;
 31980               it->phys_descent = it->descent;
 31981               it->pixel_width = font->space_width;
 31982             }
 31983 
 31984           if (it->constrain_row_ascent_descent_p)
 31985             {
 31986               if (it->descent > it->max_descent)
 31987                 {
 31988                   it->ascent += it->descent - it->max_descent;
 31989                   it->descent = it->max_descent;
 31990                 }
 31991               if (it->ascent > it->max_ascent)
 31992                 {
 31993                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 31994                   it->ascent = it->max_ascent;
 31995                 }
 31996               it->phys_ascent = min (it->phys_ascent, it->ascent);
 31997               it->phys_descent = min (it->phys_descent, it->descent);
 31998               extra_line_spacing = 0;
 31999             }
 32000 
 32001           /* If this is a space inside a region of text with
 32002              `space-width' property, change its width.  */
 32003           bool stretched_p
 32004             = it->char_to_display == ' ' && !NILP (it->space_width);
 32005           if (stretched_p)
 32006             it->pixel_width *= XFLOATINT (it->space_width);
 32007 
 32008           IT_APPLY_FACE_BOX(it, face);
 32009 
 32010           /* If face has an overline, add the height of the overline
 32011              (1 pixel) and a 1 pixel margin to the character height.  */
 32012           if (face->overline_p)
 32013             it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32014 
 32015           if (it->constrain_row_ascent_descent_p)
 32016             {
 32017               if (it->ascent > it->max_ascent)
 32018                 it->ascent = it->max_ascent;
 32019               if (it->descent > it->max_descent)
 32020                 it->descent = it->max_descent;
 32021             }
 32022 
 32023           take_vertical_position_into_account (it);
 32024 
 32025           /* If we have to actually produce glyphs, do it.  */
 32026           if (it->glyph_row)
 32027             {
 32028               if (stretched_p)
 32029                 {
 32030                   /* Translate a space with a `space-width' property
 32031                      into a stretch glyph.  */
 32032                   int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
 32033                                 / FONT_HEIGHT (font));
 32034                   append_stretch_glyph (it, it->object, it->pixel_width,
 32035                                         it->ascent + it->descent, ascent);
 32036                 }
 32037               else
 32038                 append_glyph (it);
 32039 
 32040               /* If characters with lbearing or rbearing are displayed
 32041                  in this line, record that fact in a flag of the
 32042                  glyph row.  This is used to optimize X output code.  */
 32043               if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
 32044                 it->glyph_row->contains_overlapping_glyphs_p = true;
 32045             }
 32046           if (! stretched_p && it->pixel_width == 0)
 32047             /* We assure that all visible glyphs have at least 1-pixel
 32048                width.  */
 32049             it->pixel_width = 1;
 32050         }
 32051       else if (it->char_to_display == '\n')
 32052         {
 32053           /* A newline has no width, but we need the height of the
 32054              line.  But if previous part of the line sets a height,
 32055              don't increase that height.  */
 32056 
 32057           Lisp_Object height;
 32058           Lisp_Object total_height = Qnil;
 32059 
 32060           it->override_ascent = -1;
 32061           it->pixel_width = 0;
 32062           it->nglyphs = 0;
 32063 
 32064           height = get_it_property (it, Qline_height);
 32065           /* Split (line-height total-height) list.  */
 32066           if (CONSP (height)
 32067               && CONSP (XCDR (height))
 32068               && NILP (XCDR (XCDR (height))))
 32069             {
 32070               total_height = XCAR (XCDR (height));
 32071               height = XCAR (height);
 32072             }
 32073           height = calc_line_height_property (it, height, font, boff, true);
 32074 
 32075           if (it->override_ascent >= 0)
 32076             {
 32077               it->ascent = it->override_ascent;
 32078               it->descent = it->override_descent;
 32079               boff = it->override_boff;
 32080             }
 32081           else
 32082             {
 32083               if (FONT_TOO_HIGH (font))
 32084                 {
 32085                   it->ascent = font->pixel_size + boff - 1;
 32086                   it->descent = -boff + 1;
 32087                   if (it->descent < 0)
 32088                     it->descent = 0;
 32089                 }
 32090               else
 32091                 {
 32092                   it->ascent = FONT_BASE (font) + boff;
 32093                   it->descent = FONT_DESCENT (font) - boff;
 32094                 }
 32095             }
 32096 
 32097           if (EQ (height, Qt))
 32098             {
 32099               if (it->descent > it->max_descent)
 32100                 {
 32101                   it->ascent += it->descent - it->max_descent;
 32102                   it->descent = it->max_descent;
 32103                 }
 32104               if (it->ascent > it->max_ascent)
 32105                 {
 32106                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32107                   it->ascent = it->max_ascent;
 32108                 }
 32109               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32110               it->phys_descent = min (it->phys_descent, it->descent);
 32111               it->constrain_row_ascent_descent_p = true;
 32112               extra_line_spacing = 0;
 32113             }
 32114           else
 32115             {
 32116               Lisp_Object spacing;
 32117 
 32118               it->phys_ascent = it->ascent;
 32119               it->phys_descent = it->descent;
 32120 
 32121               if ((it->max_ascent > 0 || it->max_descent > 0)
 32122                   && face->box != FACE_NO_BOX
 32123                   && face->box_horizontal_line_width > 0)
 32124                 {
 32125                   it->ascent += face->box_horizontal_line_width;
 32126                   it->descent += face->box_horizontal_line_width;
 32127                 }
 32128               if (!NILP (height)
 32129                   && XFIXNUM (height) > it->ascent + it->descent)
 32130                 it->ascent = XFIXNUM (height) - it->descent;
 32131 
 32132               if (!NILP (total_height))
 32133                 spacing = calc_line_height_property (it, total_height, font,
 32134                                                      boff, false);
 32135               else
 32136                 {
 32137                   spacing = get_it_property (it, Qline_spacing);
 32138                   spacing = calc_line_height_property (it, spacing, font,
 32139                                                        boff, false);
 32140                 }
 32141               if (FIXNUMP (spacing))
 32142                 {
 32143                   extra_line_spacing = XFIXNUM (spacing);
 32144                   if (!NILP (total_height))
 32145                     extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 32146                 }
 32147             }
 32148         }
 32149       else                    /* i.e. (it->char_to_display == '\t') */
 32150         {
 32151           if (font->space_width > 0)
 32152             {
 32153               int tab_width = it->tab_width * font->space_width;
 32154               int x = it->current_x + it->continuation_lines_width;
 32155               int x0 = x;
 32156               /* Adjust for line numbers, if needed.   */
 32157               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32158                 {
 32159                   x -= it->lnum_pixel_width;
 32160                   /* Restore the original TAB width, if required.  */
 32161                   if (x + it->stretch_adjust >= it->first_visible_x)
 32162                     x += it->stretch_adjust;
 32163                 }
 32164 
 32165               int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
 32166 
 32167               /* If the distance from the current position to the next tab
 32168                  stop is less than a space character width, use the
 32169                  tab stop after that.  */
 32170               if (next_tab_x - x < font->space_width)
 32171                 next_tab_x += tab_width;
 32172               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32173                 {
 32174                   next_tab_x += it->lnum_pixel_width;
 32175                   /* If the line is hscrolled, and the TAB starts before
 32176                      the first visible pixel, simulate negative row->x.  */
 32177                   if (x < it->first_visible_x)
 32178                     {
 32179                       next_tab_x -= it->first_visible_x - x;
 32180                       it->stretch_adjust = it->first_visible_x - x;
 32181                     }
 32182                   else
 32183                     next_tab_x -= it->stretch_adjust;
 32184                 }
 32185 
 32186               it->pixel_width = next_tab_x - x0;
 32187               it->nglyphs = 1;
 32188               if (FONT_TOO_HIGH (font))
 32189                 {
 32190                   if (get_char_glyph_code (' ', font, &char2b))
 32191                     {
 32192                       pcm = get_per_char_metric (font, &char2b);
 32193                       if (pcm->width == 0
 32194                           && pcm->rbearing == 0 && pcm->lbearing == 0)
 32195                         pcm = NULL;
 32196                     }
 32197 
 32198                   if (pcm)
 32199                     {
 32200                       it->ascent = pcm->ascent + boff;
 32201                       it->descent = pcm->descent - boff;
 32202                     }
 32203                   else
 32204                     {
 32205                       it->ascent = font->pixel_size + boff - 1;
 32206                       it->descent = -boff + 1;
 32207                     }
 32208                   if (it->ascent < 0)
 32209                     it->ascent = 0;
 32210                   if (it->descent < 0)
 32211                     it->descent = 0;
 32212                 }
 32213               else
 32214                 {
 32215                   it->ascent = FONT_BASE (font) + boff;
 32216                   it->descent = FONT_DESCENT (font) - boff;
 32217                 }
 32218               it->phys_ascent = it->ascent;
 32219               it->phys_descent = it->descent;
 32220 
 32221               if (it->glyph_row)
 32222                 {
 32223                   append_stretch_glyph (it, it->object, it->pixel_width,
 32224                                         it->ascent + it->descent, it->ascent);
 32225                 }
 32226             }
 32227           else
 32228             {
 32229               it->pixel_width = 0;
 32230               it->nglyphs = 1;
 32231             }
 32232         }
 32233 
 32234       if (FONT_TOO_HIGH (font))
 32235         {
 32236           int font_ascent, font_descent;
 32237 
 32238           /* For very large fonts, where we ignore the declared font
 32239              dimensions, and go by per-character metrics instead,
 32240              don't let the row ascent and descent values (and the row
 32241              height computed from them) be smaller than the "normal"
 32242              character metrics.  This avoids unpleasant effects
 32243              whereby lines on display would change their height
 32244              depending on which characters are shown.  */
 32245           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32246           it->max_ascent = max (it->max_ascent, font_ascent);
 32247           it->max_descent = max (it->max_descent, font_descent);
 32248         }
 32249 
 32250       if (it->ascent < 0)
 32251         it->ascent = 0;
 32252       if (it->descent < 0)
 32253         it->descent = 0;
 32254     }
 32255   else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
 32256     {
 32257       /* A static composition.
 32258 
 32259          Note: A composition is represented as one glyph in the
 32260          glyph matrix.  There are no padding glyphs.
 32261 
 32262          Important note: pixel_width, ascent, and descent are the
 32263          values of what is drawn by draw_glyphs (i.e. the values of
 32264          the overall glyphs composed).  */
 32265       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32266       int boff;                 /* baseline offset */
 32267       struct composition *cmp = composition_table[it->cmp_it.id];
 32268       int glyph_len = cmp->glyph_len;
 32269       struct font *font = face->font;
 32270 
 32271       it->nglyphs = 1;
 32272 
 32273       /* If we have not yet calculated pixel size data of glyphs of
 32274          the composition for the current face font, calculate them
 32275          now.  Theoretically, we have to check all fonts for the
 32276          glyphs, but that requires much time and memory space.  So,
 32277          here we check only the font of the first glyph.  This may
 32278          lead to incorrect display, but it's very rare, and C-l
 32279          (recenter-top-bottom) can correct the display anyway.  */
 32280       if (! cmp->font || cmp->font != font)
 32281         {
 32282           /* Ascent and descent of the font of the first character
 32283              of this composition (adjusted by baseline offset).
 32284              Ascent and descent of overall glyphs should not be less
 32285              than these, respectively.  */
 32286           int font_ascent, font_descent, font_height;
 32287           /* Bounding box of the overall glyphs.  */
 32288           int leftmost, rightmost, lowest, highest;
 32289           int lbearing, rbearing;
 32290           int i, width, ascent, descent;
 32291           int c;
 32292           unsigned char2b;
 32293           struct font_metrics *pcm;
 32294           ptrdiff_t pos;
 32295 
 32296           eassume (0 < glyph_len); /* See Bug#8512.  */
 32297           do
 32298             c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
 32299           while (c == '\t' && 0 < --glyph_len);
 32300 
 32301           bool right_padded = glyph_len < cmp->glyph_len;
 32302           for (i = 0; i < glyph_len; i++)
 32303             {
 32304               c = COMPOSITION_GLYPH (cmp, i);
 32305               if (c != '\t')
 32306                 break;
 32307               cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32308             }
 32309           bool left_padded = i > 0;
 32310 
 32311           pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
 32312                  : IT_CHARPOS (*it));
 32313           /* If no suitable font is found, use the default font.  */
 32314           bool font_not_found_p = font == NULL;
 32315           if (font_not_found_p)
 32316             {
 32317               face = face->ascii_face;
 32318               font = face->font;
 32319             }
 32320           boff = font->baseline_offset;
 32321           if (font->vertical_centering)
 32322             boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32323           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32324           font_ascent +=  boff;
 32325           font_descent -= boff;
 32326           font_height = font_ascent + font_descent;
 32327 
 32328           cmp->font = font;
 32329 
 32330           pcm = NULL;
 32331           if (! font_not_found_p)
 32332             {
 32333               get_char_face_and_encoding (it->f, c, it->face_id,
 32334                                           &char2b, false);
 32335               pcm = get_per_char_metric (font, &char2b);
 32336             }
 32337 
 32338           /* Initialize the bounding box.  */
 32339           if (pcm)
 32340             {
 32341               width = cmp->glyph_len > 0 ? pcm->width : 0;
 32342               ascent = pcm->ascent;
 32343               descent = pcm->descent;
 32344               lbearing = pcm->lbearing;
 32345               rbearing = pcm->rbearing;
 32346             }
 32347           else
 32348             {
 32349               width = cmp->glyph_len > 0 ? font->space_width : 0;
 32350               ascent = FONT_BASE (font);
 32351               descent = FONT_DESCENT (font);
 32352               lbearing = 0;
 32353               rbearing = width;
 32354             }
 32355 
 32356           rightmost = width;
 32357           leftmost = 0;
 32358           lowest = - descent + boff;
 32359           highest = ascent + boff;
 32360 
 32361           if (! font_not_found_p
 32362               && font->default_ascent
 32363               && CHAR_TABLE_P (Vuse_default_ascent)
 32364               && !NILP (Faref (Vuse_default_ascent,
 32365                                make_fixnum (it->char_to_display))))
 32366             highest = font->default_ascent + boff;
 32367 
 32368           /* Draw the first glyph at the normal position.  It may be
 32369              shifted to right later if some other glyphs are drawn
 32370              at the left.  */
 32371           cmp->offsets[i * 2] = 0;
 32372           cmp->offsets[i * 2 + 1] = boff;
 32373           cmp->lbearing = lbearing;
 32374           cmp->rbearing = rbearing;
 32375 
 32376           /* Set cmp->offsets for the remaining glyphs.  */
 32377           for (i++; i < glyph_len; i++)
 32378             {
 32379               int left, right, btm, top;
 32380               int ch = COMPOSITION_GLYPH (cmp, i);
 32381               int face_id;
 32382               struct face *this_face;
 32383 
 32384               if (ch == '\t')
 32385                 ch = ' ';
 32386               face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
 32387               this_face = FACE_FROM_ID (it->f, face_id);
 32388               font = this_face->font;
 32389 
 32390               if (font == NULL)
 32391                 pcm = NULL;
 32392               else
 32393                 {
 32394                   get_char_face_and_encoding (it->f, ch, face_id,
 32395                                               &char2b, false);
 32396                   pcm = get_per_char_metric (font, &char2b);
 32397                 }
 32398               if (! pcm)
 32399                 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32400               else
 32401                 {
 32402                   width = pcm->width;
 32403                   ascent = pcm->ascent;
 32404                   descent = pcm->descent;
 32405                   lbearing = pcm->lbearing;
 32406                   rbearing = pcm->rbearing;
 32407                   if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
 32408                     {
 32409                       /* Relative composition with or without
 32410                          alternate chars.  */
 32411                       left = (leftmost + rightmost - width) / 2;
 32412                       btm = - descent + boff;
 32413                       if (font->relative_compose
 32414                           && (! CHAR_TABLE_P (Vignore_relative_composition)
 32415                               || NILP (Faref (Vignore_relative_composition,
 32416                                               make_fixnum (ch)))))
 32417                         {
 32418 
 32419                           if (- descent >= font->relative_compose)
 32420                             /* One extra pixel between two glyphs.  */
 32421                             btm = highest + 1;
 32422                           else if (ascent <= 0)
 32423                             /* One extra pixel between two glyphs.  */
 32424                             btm = lowest - 1 - ascent - descent;
 32425                         }
 32426                     }
 32427                   else
 32428                     {
 32429                       /* A composition rule is specified by an integer
 32430                          value that encodes global and new reference
 32431                          points (GREF and NREF).  GREF and NREF are
 32432                          specified by numbers as below:
 32433 
 32434                          0---1---2 -- ascent
 32435                          |       |
 32436                          |       |
 32437                          |       |
 32438                          9--10--11 -- center
 32439                          |       |
 32440                          ---3---4---5--- baseline
 32441                          |       |
 32442                          6---7---8 -- descent
 32443                       */
 32444                       int rule = COMPOSITION_RULE (cmp, i);
 32445                       int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
 32446 
 32447                       COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
 32448                       grefx = gref % 3, nrefx = nref % 3;
 32449                       grefy = gref / 3, nrefy = nref / 3;
 32450                       if (xoff)
 32451                         xoff = font_height * (xoff - 128) / 256;
 32452                       if (yoff)
 32453                         yoff = font_height * (yoff - 128) / 256;
 32454 
 32455                       left = (leftmost
 32456                               + grefx * (rightmost - leftmost) / 2
 32457                               - nrefx * width / 2
 32458                               + xoff);
 32459 
 32460                       btm = ((grefy == 0 ? highest
 32461                               : grefy == 1 ? 0
 32462                               : grefy == 2 ? lowest
 32463                               : (highest + lowest) / 2)
 32464                              - (nrefy == 0 ? ascent + descent
 32465                                 : nrefy == 1 ? descent - boff
 32466                                 : nrefy == 2 ? 0
 32467                                 : (ascent + descent) / 2)
 32468                              + yoff);
 32469                     }
 32470 
 32471                   cmp->offsets[i * 2] = left;
 32472                   cmp->offsets[i * 2 + 1] = btm + descent;
 32473 
 32474                   /* Update the bounding box of the overall glyphs. */
 32475                   if (width > 0)
 32476                     {
 32477                       right = left + width;
 32478                       if (left < leftmost)
 32479                         leftmost = left;
 32480                       if (right > rightmost)
 32481                         rightmost = right;
 32482                     }
 32483                   top = btm + descent + ascent;
 32484                   if (top > highest)
 32485                     highest = top;
 32486                   if (btm < lowest)
 32487                     lowest = btm;
 32488 
 32489                   if (cmp->lbearing > left + lbearing)
 32490                     cmp->lbearing = left + lbearing;
 32491                   if (cmp->rbearing < left + rbearing)
 32492                     cmp->rbearing = left + rbearing;
 32493                 }
 32494             }
 32495 
 32496           /* If there are glyphs whose x-offsets are negative,
 32497              shift all glyphs to the right and make all x-offsets
 32498              non-negative.  */
 32499           if (leftmost < 0)
 32500             {
 32501               for (i = 0; i < cmp->glyph_len; i++)
 32502                 cmp->offsets[i * 2] -= leftmost;
 32503               rightmost -= leftmost;
 32504               cmp->lbearing -= leftmost;
 32505               cmp->rbearing -= leftmost;
 32506             }
 32507 
 32508           if (left_padded && cmp->lbearing < 0)
 32509             {
 32510               for (i = 0; i < cmp->glyph_len; i++)
 32511                 cmp->offsets[i * 2] -= cmp->lbearing;
 32512               rightmost -= cmp->lbearing;
 32513               cmp->rbearing -= cmp->lbearing;
 32514               cmp->lbearing = 0;
 32515             }
 32516           if (right_padded && rightmost < cmp->rbearing)
 32517             {
 32518               rightmost = cmp->rbearing;
 32519             }
 32520 
 32521           cmp->pixel_width = rightmost;
 32522           cmp->ascent = highest;
 32523           cmp->descent = - lowest;
 32524           if (cmp->ascent < font_ascent)
 32525             cmp->ascent = font_ascent;
 32526           if (cmp->descent < font_descent)
 32527             cmp->descent = font_descent;
 32528         }
 32529 
 32530       if (it->glyph_row
 32531           && (cmp->lbearing < 0
 32532               || cmp->rbearing > cmp->pixel_width))
 32533         it->glyph_row->contains_overlapping_glyphs_p = true;
 32534 
 32535       it->pixel_width = cmp->pixel_width;
 32536       it->ascent = it->phys_ascent = cmp->ascent;
 32537       it->descent = it->phys_descent = cmp->descent;
 32538       IT_APPLY_FACE_BOX(it, face);
 32539 
 32540       /* If face has an overline, add the height of the overline
 32541          (1 pixel) and a 1 pixel margin to the character height.  */
 32542       if (face->overline_p)
 32543         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32544 
 32545       take_vertical_position_into_account (it);
 32546       if (it->ascent < 0)
 32547         it->ascent = 0;
 32548       if (it->descent < 0)
 32549         it->descent = 0;
 32550 
 32551       if (it->glyph_row && cmp->glyph_len > 0)
 32552         append_composite_glyph (it);
 32553     }
 32554   else if (it->what == IT_COMPOSITION)
 32555     {
 32556       /* A dynamic (automatic) composition.  */
 32557       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32558       Lisp_Object gstring;
 32559       struct font_metrics metrics;
 32560 
 32561       it->nglyphs = 1;
 32562 
 32563       gstring = composition_gstring_from_id (it->cmp_it.id);
 32564       it->pixel_width
 32565         = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
 32566                                      &metrics);
 32567       if (it->pixel_width == 0)
 32568         {
 32569           it->glyph_not_available_p = true;
 32570           it->phys_ascent = it->ascent;
 32571           it->phys_descent = it->descent;
 32572           it->pixel_width = face->font->space_width;
 32573         }
 32574       else
 32575         {
 32576           if (it->glyph_row
 32577               && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
 32578             it->glyph_row->contains_overlapping_glyphs_p = true;
 32579           it->ascent = it->phys_ascent = metrics.ascent;
 32580           it->descent = it->phys_descent = metrics.descent;
 32581         }
 32582       IT_APPLY_FACE_BOX(it, face);
 32583 
 32584       /* If face has an overline, add the height of the overline
 32585          (1 pixel) and a 1 pixel margin to the character height.  */
 32586       if (face->overline_p)
 32587         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32588       take_vertical_position_into_account (it);
 32589       if (it->ascent < 0)
 32590         it->ascent = 0;
 32591       if (it->descent < 0)
 32592         it->descent = 0;
 32593 
 32594       if (it->glyph_row)
 32595         append_composite_glyph (it);
 32596     }
 32597   else if (it->what == IT_GLYPHLESS)
 32598     produce_glyphless_glyph (it, false, Qnil);
 32599   else if (it->what == IT_IMAGE)
 32600     produce_image_glyph (it);
 32601   else if (it->what == IT_STRETCH)
 32602     produce_stretch_glyph (it);
 32603   else if (it->what == IT_XWIDGET)
 32604     produce_xwidget_glyph (it);
 32605 
 32606  done:
 32607   /* Accumulate dimensions.  Note: can't assume that it->descent > 0
 32608      because this isn't true for images with `:ascent 100'.  */
 32609   eassert (it->ascent >= 0 && it->descent >= 0);
 32610   if (it->area == TEXT_AREA)
 32611     it->current_x += it->pixel_width;
 32612 
 32613   if (extra_line_spacing > 0)
 32614     {
 32615       it->descent += extra_line_spacing;
 32616       if (extra_line_spacing > it->max_extra_line_spacing)
 32617         it->max_extra_line_spacing = extra_line_spacing;
 32618     }
 32619 
 32620   it->max_ascent = max (it->max_ascent, it->ascent);
 32621   it->max_descent = max (it->max_descent, it->descent);
 32622   it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
 32623   it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
 32624 }
 32625 
 32626 /* EXPORT for RIF:
 32627    Output LEN glyphs starting at START at the nominal cursor position.
 32628    Advance the nominal cursor over the text.  UPDATED_ROW is the glyph row
 32629    being updated, and UPDATED_AREA is the area of that row being updated.  */
 32630 
 32631 void
 32632 gui_write_glyphs (struct window *w, struct glyph_row *updated_row,
 32633                 struct glyph *start, enum glyph_row_area updated_area, int len)
 32634 {
 32635   int x, hpos, chpos = w->phys_cursor.hpos;
 32636 
 32637   eassert (updated_row);
 32638   /* When the window is hscrolled, cursor hpos can legitimately be out
 32639      of bounds, but we draw the cursor at the corresponding window
 32640      margin in that case.  */
 32641   if (!updated_row->reversed_p && chpos < 0)
 32642     chpos = 0;
 32643   if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
 32644     chpos = updated_row->used[TEXT_AREA] - 1;
 32645 
 32646   block_input ();
 32647 
 32648   /* Write glyphs.  */
 32649 
 32650   hpos = start - updated_row->glyphs[updated_area];
 32651   x = draw_glyphs (w, w->output_cursor.x,
 32652                    updated_row, updated_area,
 32653                    hpos, hpos + len,
 32654                    DRAW_NORMAL_TEXT, 0);
 32655 
 32656   /* Invalidate old phys cursor if the glyph at its hpos is redrawn.  */
 32657   if (updated_area == TEXT_AREA
 32658       && w->phys_cursor_on_p
 32659       && w->phys_cursor.vpos == w->output_cursor.vpos
 32660       && chpos >= hpos
 32661       && chpos < hpos + len)
 32662     w->phys_cursor_on_p = false;
 32663 
 32664   unblock_input ();
 32665 
 32666   /* Advance the output cursor.  */
 32667   w->output_cursor.hpos += len;
 32668   w->output_cursor.x = x;
 32669 }
 32670 
 32671 
 32672 /* EXPORT for RIF:
 32673    Insert LEN glyphs from START at the nominal cursor position.  */
 32674 
 32675 void
 32676 gui_insert_glyphs (struct window *w, struct glyph_row *updated_row,
 32677                  struct glyph *start, enum glyph_row_area updated_area, int len)
 32678 {
 32679   struct frame *f;
 32680   int line_height, shift_by_width, shifted_region_width;
 32681   struct glyph_row *row;
 32682   struct glyph *glyph;
 32683   int frame_x, frame_y;
 32684   ptrdiff_t hpos;
 32685 
 32686   eassert (updated_row);
 32687   block_input ();
 32688   f = XFRAME (WINDOW_FRAME (w));
 32689 
 32690   /* Get the height of the line we are in.  */
 32691   row = updated_row;
 32692   line_height = row->height;
 32693 
 32694   /* Get the width of the glyphs to insert.  */
 32695   shift_by_width = 0;
 32696   for (glyph = start; glyph < start + len; ++glyph)
 32697     shift_by_width += glyph->pixel_width;
 32698 
 32699   /* Get the width of the region to shift right.  */
 32700   shifted_region_width = (window_box_width (w, updated_area)
 32701                           - w->output_cursor.x
 32702                           - shift_by_width);
 32703 
 32704   /* Shift right.  */
 32705   frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
 32706   frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
 32707 
 32708   FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
 32709                                           line_height, shift_by_width);
 32710 
 32711   /* Write the glyphs.  */
 32712   hpos = start - row->glyphs[updated_area];
 32713   draw_glyphs (w, w->output_cursor.x, row, updated_area,
 32714                hpos, hpos + len,
 32715                DRAW_NORMAL_TEXT, 0);
 32716 
 32717   /* Advance the output cursor.  */
 32718   w->output_cursor.hpos += len;
 32719   w->output_cursor.x += shift_by_width;
 32720   unblock_input ();
 32721 }
 32722 
 32723 
 32724 /* EXPORT for RIF:
 32725    Erase the current text line from the nominal cursor position
 32726    (inclusive) to pixel column TO_X (exclusive).  The idea is that
 32727    everything from TO_X onward is already erased.
 32728 
 32729    TO_X is a pixel position relative to UPDATED_AREA of currently
 32730    updated window W.  TO_X == -1 means clear to the end of this area.  */
 32731 
 32732 void
 32733 gui_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
 32734                        enum glyph_row_area updated_area, int to_x)
 32735 {
 32736   struct frame *f;
 32737   int max_x, min_y, max_y;
 32738   int from_x, from_y, to_y;
 32739   struct face *face;
 32740 
 32741   eassert (updated_row);
 32742   f = XFRAME (w->frame);
 32743   face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
 32744 
 32745   if (updated_row->full_width_p)
 32746     max_x = (WINDOW_PIXEL_WIDTH (w)
 32747              - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 32748   else
 32749     max_x = window_box_width (w, updated_area);
 32750   max_y = window_text_bottom_y (w);
 32751 
 32752   /* TO_X == 0 means don't do anything.  TO_X < 0 means clear to end
 32753      of window.  For TO_X > 0, truncate to end of drawing area.  */
 32754   if (to_x == 0)
 32755     return;
 32756   else if (to_x < 0)
 32757     to_x = max_x;
 32758   else
 32759     to_x = min (to_x, max_x);
 32760 
 32761   to_y = min (max_y, w->output_cursor.y + updated_row->height);
 32762 
 32763   /* Notice if the cursor will be cleared by this operation.  */
 32764   if (!updated_row->full_width_p)
 32765     notice_overwritten_cursor (w, updated_area,
 32766                                w->output_cursor.x, -1,
 32767                                updated_row->y,
 32768                                MATRIX_ROW_BOTTOM_Y (updated_row));
 32769 
 32770   from_x = w->output_cursor.x;
 32771 
 32772   /* Translate to frame coordinates.  */
 32773   if (updated_row->full_width_p)
 32774     {
 32775       from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
 32776       to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
 32777     }
 32778   else
 32779     {
 32780       int area_left = window_box_left (w, updated_area);
 32781       from_x += area_left;
 32782       to_x += area_left;
 32783     }
 32784 
 32785   min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 32786   from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
 32787   to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
 32788 
 32789   /* Prevent inadvertently clearing to end of the X window.  */
 32790   if (to_x > from_x && to_y > from_y)
 32791     {
 32792       block_input ();
 32793       FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
 32794                                        to_x - from_x, to_y - from_y);
 32795 
 32796       if (face && !updated_row->stipple_p)
 32797         updated_row->stipple_p = face->stipple;
 32798       unblock_input ();
 32799     }
 32800 }
 32801 
 32802 #endif /* HAVE_WINDOW_SYSTEM */
 32803 
 32804 
 32805 
 32806 /***********************************************************************
 32807                              Cursor types
 32808  ***********************************************************************/
 32809 
 32810 /* Value is the internal representation of the specified cursor type
 32811    ARG.  If type is BAR_CURSOR, return in *WIDTH the specified width
 32812    of the bar cursor.  */
 32813 
 32814 static enum text_cursor_kinds
 32815 get_specified_cursor_type (Lisp_Object arg, int *width)
 32816 {
 32817   enum text_cursor_kinds type;
 32818 
 32819   if (NILP (arg))
 32820     return NO_CURSOR;
 32821 
 32822   if (EQ (arg, Qbox))
 32823     return FILLED_BOX_CURSOR;
 32824 
 32825   if (EQ (arg, Qhollow))
 32826     return HOLLOW_BOX_CURSOR;
 32827 
 32828   if (EQ (arg, Qbar))
 32829     {
 32830       *width = 2;
 32831       return BAR_CURSOR;
 32832     }
 32833 
 32834   if (EQ (arg, Qhbar))
 32835     {
 32836       *width = 2;
 32837       return HBAR_CURSOR;
 32838     }
 32839 
 32840   if (CONSP (arg)
 32841       && RANGED_FIXNUMP (0, XCDR (arg), INT_MAX))
 32842     {
 32843       *width = XFIXNUM (XCDR (arg));
 32844 
 32845       if (EQ (XCAR (arg), Qbox))
 32846           return FILLED_BOX_CURSOR;
 32847       else if (EQ (XCAR (arg), Qbar))
 32848           return BAR_CURSOR;
 32849       else if (EQ (XCAR (arg), Qhbar))
 32850           return HBAR_CURSOR;
 32851     }
 32852 
 32853   /* Treat anything unknown as "hollow box cursor".
 32854      It was bad to signal an error; people have trouble fixing
 32855      .Xdefaults with Emacs, when it has something bad in it.  */
 32856   type = HOLLOW_BOX_CURSOR;
 32857 
 32858   return type;
 32859 }
 32860 
 32861 /* Set the default cursor types for specified frame.  */
 32862 void
 32863 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
 32864 {
 32865   int width = 1;
 32866   Lisp_Object tem;
 32867 
 32868   FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
 32869   FRAME_CURSOR_WIDTH (f) = width;
 32870 
 32871   /* By default, set up the blink-off state depending on the on-state.  */
 32872 
 32873   tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
 32874   if (!NILP (tem))
 32875     {
 32876       FRAME_BLINK_OFF_CURSOR (f)
 32877         = get_specified_cursor_type (XCDR (tem), &width);
 32878       FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
 32879     }
 32880   else
 32881     FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
 32882 
 32883   /* Make sure the cursor gets redrawn.  */
 32884   f->cursor_type_changed = true;
 32885 }
 32886 
 32887 
 32888 #ifdef HAVE_WINDOW_SYSTEM
 32889 
 32890 /* Return the cursor we want to be displayed in window W.  Return
 32891    width of bar/hbar cursor through WIDTH arg.  Return with
 32892    ACTIVE_CURSOR arg set to true if cursor in window W is `active'
 32893    (i.e. if the `system caret' should track this cursor).
 32894 
 32895    In a mini-buffer window, we want the cursor only to appear if we
 32896    are reading input from this window.  For the selected window, we
 32897    want the cursor type given by the frame parameter or buffer local
 32898    setting of cursor-type.  If explicitly marked off, draw no cursor.
 32899    In all other cases, we want a hollow box cursor.  */
 32900 
 32901 static enum text_cursor_kinds
 32902 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
 32903                         bool *active_cursor)
 32904 {
 32905   struct frame *f = XFRAME (w->frame);
 32906   struct buffer *b = XBUFFER (w->contents);
 32907   int cursor_type = DEFAULT_CURSOR;
 32908   Lisp_Object alt_cursor;
 32909   bool non_selected = false;
 32910 
 32911   *active_cursor = true;
 32912 
 32913   /* Echo area */
 32914   if (cursor_in_echo_area
 32915       && FRAME_HAS_MINIBUF_P (f)
 32916       && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
 32917     {
 32918       if (w == XWINDOW (echo_area_window))
 32919         {
 32920           if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
 32921             {
 32922               *width = FRAME_CURSOR_WIDTH (f);
 32923               return FRAME_DESIRED_CURSOR (f);
 32924             }
 32925           else
 32926             return get_specified_cursor_type (BVAR (b, cursor_type), width);
 32927         }
 32928 
 32929       *active_cursor = false;
 32930       non_selected = true;
 32931     }
 32932 
 32933   /* Detect a nonselected window or nonselected frame.  */
 32934   else if (w != XWINDOW (f->selected_window)
 32935            || f != FRAME_DISPLAY_INFO (f)->highlight_frame)
 32936     {
 32937       *active_cursor = false;
 32938 
 32939       if (MINI_WINDOW_P (w) &&
 32940           (minibuf_level == 0
 32941            || is_minibuffer (0, w->contents)))
 32942         return NO_CURSOR;
 32943 
 32944       non_selected = true;
 32945     }
 32946 
 32947   /* Never display a cursor in a window in which cursor-type is nil.  */
 32948   if (NILP (BVAR (b, cursor_type)))
 32949     return NO_CURSOR;
 32950 
 32951   /* Get the normal cursor type for this window.  */
 32952   if (EQ (BVAR (b, cursor_type), Qt))
 32953     {
 32954       cursor_type = FRAME_DESIRED_CURSOR (f);
 32955       *width = FRAME_CURSOR_WIDTH (f);
 32956     }
 32957   else
 32958     cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
 32959 
 32960   /* Use cursor-in-non-selected-windows instead
 32961      for non-selected window or frame.  */
 32962   if (non_selected)
 32963     {
 32964       alt_cursor = BVAR (b, cursor_in_non_selected_windows);
 32965       if (!EQ (Qt, alt_cursor))
 32966         return get_specified_cursor_type (alt_cursor, width);
 32967       /* t means modify the normal cursor type.  */
 32968       if (cursor_type == FILLED_BOX_CURSOR)
 32969         cursor_type = HOLLOW_BOX_CURSOR;
 32970       else if (cursor_type == BAR_CURSOR && *width > 1)
 32971         --*width;
 32972       return cursor_type;
 32973     }
 32974 
 32975   /* Use normal cursor if not blinked off.  */
 32976   if (!w->cursor_off_p)
 32977     {
 32978       if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
 32979         return NO_CURSOR;
 32980       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 32981         {
 32982           if (cursor_type == FILLED_BOX_CURSOR)
 32983             {
 32984               /* Using a block cursor on large images can be very
 32985                  annoying.  So use a hollow cursor for "large" images.
 32986                  If image is not transparent (no mask), also use
 32987                  hollow cursor.  */
 32988               struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 32989               if (img != NULL && IMAGEP (img->spec))
 32990                 {
 32991                   /* Interpret "large" as >SIZExSIZE and >NxN where
 32992                      SIZE is the value from cursor-type of the form
 32993                      (box . SIZE), where N = size of default frame
 32994                      font size.  So, setting cursor-type to (box . 32)
 32995                      should cover most of the "tiny" icons people may
 32996                      use.  */
 32997                   if (!img->mask
 32998                       || (CONSP (BVAR (b, cursor_type))
 32999                           && img->width > max (*width, WINDOW_FRAME_COLUMN_WIDTH (w))
 33000                           && img->height > max (*width, WINDOW_FRAME_LINE_HEIGHT (w))))
 33001                     cursor_type = HOLLOW_BOX_CURSOR;
 33002                 }
 33003             }
 33004           else if (cursor_type != NO_CURSOR)
 33005             {
 33006               /* Display current only supports BOX and HOLLOW cursors for images.
 33007                  So for now, unconditionally use a HOLLOW cursor when cursor is
 33008                  not a solid box cursor.  */
 33009               cursor_type = HOLLOW_BOX_CURSOR;
 33010             }
 33011       }
 33012       return cursor_type;
 33013     }
 33014 
 33015   /* Cursor is blinked off, so determine how to "toggle" it.  */
 33016 
 33017   /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist.  */
 33018   if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
 33019     return get_specified_cursor_type (XCDR (alt_cursor), width);
 33020 
 33021   /* Then see if frame has specified a specific blink off cursor type.  */
 33022   if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
 33023     {
 33024       *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
 33025       return FRAME_BLINK_OFF_CURSOR (f);
 33026     }
 33027 
 33028 #if false
 33029   /* Some people liked having a permanently visible blinking cursor,
 33030      while others had very strong opinions against it.  So it was
 33031      decided to remove it.  KFS 2003-09-03 */
 33032 
 33033   /* Finally perform built-in cursor blinking:
 33034        filled box      <->   hollow box
 33035        wide [h]bar     <->   narrow [h]bar
 33036        narrow [h]bar   <->   no cursor
 33037        other type      <->   no cursor  */
 33038 
 33039   if (cursor_type == FILLED_BOX_CURSOR)
 33040     return HOLLOW_BOX_CURSOR;
 33041 
 33042   if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
 33043     {
 33044       *width = 1;
 33045       return cursor_type;
 33046     }
 33047 #endif
 33048 
 33049   return NO_CURSOR;
 33050 }
 33051 
 33052 
 33053 /* Notice when the text cursor of window W has been completely
 33054    overwritten by a drawing operation that outputs glyphs in AREA
 33055    starting at X0 and ending at X1 in the line starting at Y0 and
 33056    ending at Y1.  X coordinates are area-relative.  X1 < 0 means all
 33057    the rest of the line after X0 has been written.  Y coordinates
 33058    are window-relative.  */
 33059 
 33060 static void
 33061 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
 33062                            int x0, int x1, int y0, int y1)
 33063 {
 33064   int cx0, cx1, cy0, cy1;
 33065   struct glyph_row *row;
 33066 
 33067   if (!w->phys_cursor_on_p)
 33068     return;
 33069   if (area != TEXT_AREA)
 33070     return;
 33071 
 33072   if (w->phys_cursor.vpos < 0
 33073       || w->phys_cursor.vpos >= w->current_matrix->nrows
 33074       || (row = w->current_matrix->rows + w->phys_cursor.vpos,
 33075           !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
 33076     return;
 33077 
 33078   if (row->cursor_in_fringe_p)
 33079     {
 33080       row->cursor_in_fringe_p = false;
 33081       draw_fringe_bitmap (w, row, row->reversed_p);
 33082       w->phys_cursor_on_p = false;
 33083       return;
 33084     }
 33085 
 33086   cx0 = w->phys_cursor.x;
 33087   cx1 = cx0 + w->phys_cursor_width;
 33088   if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
 33089     return;
 33090 
 33091   /* The cursor image will be completely removed from the
 33092      screen if the output area intersects the cursor area in
 33093      y-direction.  When we draw in [y0 y1[, and some part of
 33094      the cursor is at y < y0, that part must have been drawn
 33095      before.  When scrolling, the cursor is erased before
 33096      actually scrolling, so we don't come here.  When not
 33097      scrolling, the rows above the old cursor row must have
 33098      changed, and in this case these rows must have written
 33099      over the cursor image.
 33100 
 33101      Likewise if part of the cursor is below y1, with the
 33102      exception of the cursor being in the first blank row at
 33103      the buffer and window end because update_text_area
 33104      doesn't draw that row.  (Except when it does, but
 33105      that's handled in update_text_area.)  */
 33106 
 33107   cy0 = w->phys_cursor.y;
 33108   cy1 = cy0 + w->phys_cursor_height;
 33109   if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
 33110     return;
 33111 
 33112   w->phys_cursor_on_p = false;
 33113 }
 33114 
 33115 #endif /* HAVE_WINDOW_SYSTEM */
 33116 
 33117 
 33118 /************************************************************************
 33119                               Mouse Face
 33120  ************************************************************************/
 33121 
 33122 #ifdef HAVE_WINDOW_SYSTEM
 33123 
 33124 /* EXPORT for RIF:
 33125    Fix the display of area AREA of overlapping row ROW in window W
 33126    with respect to the overlapping part OVERLAPS.  */
 33127 
 33128 void
 33129 gui_fix_overlapping_area (struct window *w, struct glyph_row *row,
 33130                         enum glyph_row_area area, int overlaps)
 33131 {
 33132   int i, x;
 33133 
 33134   block_input ();
 33135 
 33136   x = 0;
 33137   for (i = 0; i < row->used[area];)
 33138     {
 33139       if (row->glyphs[area][i].overlaps_vertically_p)
 33140         {
 33141           int start = i, start_x = x;
 33142 
 33143           do
 33144             {
 33145               x += row->glyphs[area][i].pixel_width;
 33146               ++i;
 33147             }
 33148           while (i < row->used[area]
 33149                  && row->glyphs[area][i].overlaps_vertically_p);
 33150 
 33151           draw_glyphs (w, start_x, row, area,
 33152                        start, i,
 33153                        DRAW_NORMAL_TEXT, overlaps);
 33154         }
 33155       else
 33156         {
 33157           x += row->glyphs[area][i].pixel_width;
 33158           ++i;
 33159         }
 33160     }
 33161 
 33162   unblock_input ();
 33163 }
 33164 
 33165 
 33166 /* EXPORT:
 33167    Draw the cursor glyph of window W in glyph row ROW.  See the
 33168    comment of draw_glyphs for the meaning of HL.  */
 33169 
 33170 void
 33171 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
 33172                         enum draw_glyphs_face hl)
 33173 {
 33174   /* If cursor hpos is out of bounds, don't draw garbage.  This can
 33175      happen in mini-buffer windows when switching between echo area
 33176      glyphs and mini-buffer.  */
 33177   if ((row->reversed_p
 33178        ? (w->phys_cursor.hpos >= 0)
 33179        : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
 33180     {
 33181       bool on_p = w->phys_cursor_on_p;
 33182       int x1;
 33183       int hpos = w->phys_cursor.hpos;
 33184 
 33185       /* When the window is hscrolled, cursor hpos can legitimately be
 33186          out of bounds, but we draw the cursor at the corresponding
 33187          window margin in that case.  */
 33188       if (!row->reversed_p && hpos < 0)
 33189         hpos = 0;
 33190       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33191         hpos = row->used[TEXT_AREA] - 1;
 33192 
 33193       x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
 33194                         hl, 0);
 33195       w->phys_cursor_on_p = on_p;
 33196 
 33197       if (hl == DRAW_CURSOR)
 33198         w->phys_cursor_width = x1 - w->phys_cursor.x;
 33199       /* When we erase the cursor, and ROW is overlapped by other
 33200          rows, make sure that these overlapping parts of other rows
 33201          are redrawn.  */
 33202       else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
 33203         {
 33204           w->phys_cursor_width = x1 - w->phys_cursor.x;
 33205 
 33206           if (row > w->current_matrix->rows
 33207               && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
 33208             gui_fix_overlapping_area (w, row - 1, TEXT_AREA,
 33209                                     OVERLAPS_ERASED_CURSOR);
 33210 
 33211           if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
 33212               && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
 33213             gui_fix_overlapping_area (w, row + 1, TEXT_AREA,
 33214                                     OVERLAPS_ERASED_CURSOR);
 33215         }
 33216     }
 33217 }
 33218 
 33219 
 33220 /* Erase the image of a cursor of window W from the screen.  */
 33221 
 33222 void
 33223 erase_phys_cursor (struct window *w)
 33224 {
 33225   struct frame *f = XFRAME (w->frame);
 33226   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 33227   int hpos = w->phys_cursor.hpos;
 33228   int vpos = w->phys_cursor.vpos;
 33229   bool mouse_face_here_p = false;
 33230   struct glyph_matrix *active_glyphs = w->current_matrix;
 33231   struct glyph_row *cursor_row;
 33232   struct glyph *cursor_glyph;
 33233   enum draw_glyphs_face hl;
 33234 
 33235   /* No cursor displayed or row invalidated => nothing to do on the
 33236      screen.  */
 33237   if (w->phys_cursor_type == NO_CURSOR)
 33238     goto mark_cursor_off;
 33239 
 33240   /* VPOS >= active_glyphs->nrows means that window has been resized.
 33241      Don't bother to erase the cursor.  */
 33242   if (vpos >= active_glyphs->nrows)
 33243     goto mark_cursor_off;
 33244 
 33245   /* If row containing cursor is marked invalid, there is nothing we
 33246      can do.  */
 33247   cursor_row = MATRIX_ROW (active_glyphs, vpos);
 33248   if (!cursor_row->enabled_p)
 33249     goto mark_cursor_off;
 33250 
 33251   /* If line spacing is > 0, old cursor may only be partially visible in
 33252      window after split-window.  So adjust visible height.  */
 33253   cursor_row->visible_height = min (cursor_row->visible_height,
 33254                                     window_text_bottom_y (w) - cursor_row->y);
 33255 
 33256   /* If row is completely invisible, don't attempt to delete a cursor which
 33257      isn't there.  This can happen if cursor is at top of a window, and
 33258      we switch to a buffer with a header line in that window.  */
 33259   if (cursor_row->visible_height <= 0)
 33260     goto mark_cursor_off;
 33261 
 33262   /* If cursor is in the fringe, erase by drawing actual bitmap there.  */
 33263   if (cursor_row->cursor_in_fringe_p)
 33264     {
 33265       cursor_row->cursor_in_fringe_p = false;
 33266       draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
 33267       goto mark_cursor_off;
 33268     }
 33269 
 33270   /* This can happen when the new row is shorter than the old one.
 33271      In this case, either draw_glyphs or clear_end_of_line
 33272      should have cleared the cursor.  Note that we wouldn't be
 33273      able to erase the cursor in this case because we don't have a
 33274      cursor glyph at hand.  */
 33275   if ((cursor_row->reversed_p
 33276        ? (w->phys_cursor.hpos < 0)
 33277        : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
 33278     goto mark_cursor_off;
 33279 
 33280   /* When the window is hscrolled, cursor hpos can legitimately be out
 33281      of bounds, but we draw the cursor at the corresponding window
 33282      margin in that case.  */
 33283   if (!cursor_row->reversed_p && hpos < 0)
 33284     hpos = 0;
 33285   if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
 33286     hpos = cursor_row->used[TEXT_AREA] - 1;
 33287 
 33288   /* If the cursor is in the mouse face area, redisplay that when
 33289      we clear the cursor.  */
 33290   if (! NILP (hlinfo->mouse_face_window)
 33291       && coords_in_mouse_face_p (w, hpos, vpos)
 33292       /* Don't redraw the cursor's spot in mouse face if it is at the
 33293          end of a line (on a newline).  The cursor appears there, but
 33294          mouse highlighting does not.  */
 33295       && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
 33296     mouse_face_here_p = true;
 33297 
 33298 #ifdef HAVE_WINDOW_SYSTEM
 33299   /* Since erasing the phys cursor will probably lead to corruption of
 33300      the mouse face display if the glyph's pixel_width is not kept up
 33301      to date with the :box property of the mouse face, just redraw the
 33302      mouse face.  */
 33303   if (FRAME_WINDOW_P (WINDOW_XFRAME (w)) && mouse_face_here_p)
 33304     {
 33305       w->phys_cursor_on_p = false;
 33306       w->phys_cursor_type = NO_CURSOR;
 33307       show_mouse_face (MOUSE_HL_INFO (WINDOW_XFRAME (w)), DRAW_MOUSE_FACE);
 33308       return;
 33309     }
 33310 #endif
 33311 
 33312   /* Maybe clear the display under the cursor.  */
 33313   if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
 33314     {
 33315       int x, y;
 33316       int tab_line_height = WINDOW_TAB_LINE_HEIGHT (w);
 33317       int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
 33318       int width;
 33319 
 33320       cursor_glyph = get_phys_cursor_glyph (w);
 33321       if (cursor_glyph == NULL)
 33322         goto mark_cursor_off;
 33323 
 33324       width = cursor_glyph->pixel_width;
 33325       x = w->phys_cursor.x;
 33326       if (x < 0)
 33327         {
 33328           width += x;
 33329           x = 0;
 33330         }
 33331       width = min (width, window_box_width (w, TEXT_AREA) - x);
 33332       y = WINDOW_TO_FRAME_PIXEL_Y (w, max (tab_line_height, max (header_line_height, cursor_row->y)));
 33333       x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
 33334 
 33335       if (width > 0)
 33336         FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
 33337     }
 33338 
 33339   /* Erase the cursor by redrawing the character underneath it.  */
 33340   if (mouse_face_here_p)
 33341     hl = DRAW_MOUSE_FACE;
 33342   else
 33343     hl = DRAW_NORMAL_TEXT;
 33344   draw_phys_cursor_glyph (w, cursor_row, hl);
 33345 
 33346  mark_cursor_off:
 33347   w->phys_cursor_on_p = false;
 33348   w->phys_cursor_type = NO_CURSOR;
 33349 }
 33350 
 33351 
 33352 /* Display or clear cursor of window W.  If !ON, clear the cursor.
 33353    If ON, display the cursor; where to put the cursor is specified by
 33354    HPOS, VPOS, X and Y.  */
 33355 
 33356 void
 33357 display_and_set_cursor (struct window *w, bool on,
 33358                         int hpos, int vpos, int x, int y)
 33359 {
 33360   struct frame *f = XFRAME (w->frame);
 33361   int new_cursor_type;
 33362   int new_cursor_width UNINIT;
 33363   bool active_cursor;
 33364   struct glyph_row *glyph_row;
 33365   struct glyph *glyph;
 33366 
 33367   /* This is pointless on invisible frames, and dangerous on garbaged
 33368      windows and frames; in the latter case, the frame or window may
 33369      be in the midst of changing its size, and x and y may be off the
 33370      window.  */
 33371   if (! FRAME_VISIBLE_P (f)
 33372       || vpos >= w->current_matrix->nrows
 33373       || hpos >= w->current_matrix->matrix_w)
 33374     return;
 33375 
 33376   /* If cursor is off and we want it off, return quickly.  */
 33377   if (!on && !w->phys_cursor_on_p)
 33378     return;
 33379 
 33380   glyph_row = MATRIX_ROW (w->current_matrix, vpos);
 33381   /* If cursor row is not enabled, we don't really know where to
 33382      display the cursor.  */
 33383   if (!glyph_row->enabled_p)
 33384     {
 33385       w->phys_cursor_on_p = false;
 33386       return;
 33387     }
 33388 
 33389   /* A frame might be marked garbaged even though its cursor position
 33390      is correct, and will not change upon subsequent redisplay.  This
 33391      happens in some rare situations, like toggling the sort order in
 33392      Dired windows.  We've already established that VPOS is valid, so
 33393      it shouldn't do any harm to record the cursor position, as we are
 33394      going to return without acting on it anyway.  Otherwise, expose
 33395      events might come in and call update_window_cursor, which will
 33396      blindly use outdated values in w->phys_cursor.  */
 33397   if (FRAME_GARBAGED_P (f))
 33398     {
 33399       if (on)
 33400         {
 33401           w->phys_cursor.x = x;
 33402           w->phys_cursor.y = glyph_row->y;
 33403           w->phys_cursor.hpos = hpos;
 33404           w->phys_cursor.vpos = vpos;
 33405         }
 33406       return;
 33407     }
 33408 
 33409   glyph = NULL;
 33410   if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
 33411     glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
 33412 
 33413   eassert (input_blocked_p ());
 33414 
 33415   /* Set new_cursor_type to the cursor we want to be displayed.  */
 33416   new_cursor_type = get_window_cursor_type (w, glyph,
 33417                                             &new_cursor_width, &active_cursor);
 33418 
 33419   /* If cursor is currently being shown and we don't want it to be or
 33420      it is in the wrong place, or the cursor type is not what we want,
 33421      erase it.  */
 33422   if (w->phys_cursor_on_p
 33423       && (!on
 33424           || w->phys_cursor.x != x
 33425           || w->phys_cursor.y != y
 33426           /* HPOS can be negative in R2L rows whose
 33427              exact_window_width_line_p flag is set (i.e. their newline
 33428              would "overflow into the fringe").  */
 33429           || hpos < 0
 33430           || new_cursor_type != w->phys_cursor_type
 33431           || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
 33432               && new_cursor_width != w->phys_cursor_width)))
 33433     erase_phys_cursor (w);
 33434 
 33435   /* Don't check phys_cursor_on_p here because that flag is only set
 33436      to false in some cases where we know that the cursor has been
 33437      completely erased, to avoid the extra work of erasing the cursor
 33438      twice.  In other words, phys_cursor_on_p can be true and the cursor
 33439      still not be visible, or it has only been partly erased.  */
 33440   if (on)
 33441     {
 33442       w->phys_cursor_ascent = glyph_row->ascent;
 33443       w->phys_cursor_height = glyph_row->height;
 33444 
 33445       /* Set phys_cursor_.* before x_draw_.* is called because some
 33446          of them may need the information.  */
 33447       w->phys_cursor.x = x;
 33448       w->phys_cursor.y = glyph_row->y;
 33449       w->phys_cursor.hpos = hpos;
 33450       w->phys_cursor.vpos = vpos;
 33451     }
 33452 
 33453   FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
 33454                                      new_cursor_type, new_cursor_width,
 33455                                      on, active_cursor);
 33456 }
 33457 
 33458 
 33459 /* Switch the display of W's cursor on or off, according to the value
 33460    of ON.  */
 33461 
 33462 static void
 33463 update_window_cursor (struct window *w, bool on)
 33464 {
 33465   /* Don't update cursor in windows whose frame is in the process
 33466      of being deleted.  */
 33467   if (w->current_matrix)
 33468     {
 33469       int hpos = w->phys_cursor.hpos;
 33470       int vpos = w->phys_cursor.vpos;
 33471       struct glyph_row *row;
 33472 
 33473       if (vpos >= w->current_matrix->nrows
 33474           || hpos >= w->current_matrix->matrix_w)
 33475         return;
 33476 
 33477       row = MATRIX_ROW (w->current_matrix, vpos);
 33478 
 33479       /* When the window is hscrolled, cursor hpos can legitimately be
 33480          out of bounds, but we draw the cursor at the corresponding
 33481          window margin in that case.  */
 33482       if (!row->reversed_p && hpos < 0)
 33483         hpos = 0;
 33484       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33485         hpos = row->used[TEXT_AREA] - 1;
 33486 
 33487       block_input ();
 33488       display_and_set_cursor (w, on, hpos, vpos,
 33489                               w->phys_cursor.x, w->phys_cursor.y);
 33490       unblock_input ();
 33491     }
 33492 }
 33493 
 33494 
 33495 /* Call update_window_cursor with parameter ON_P on all leaf windows
 33496    in the window tree rooted at W.  */
 33497 
 33498 static void
 33499 update_cursor_in_window_tree (struct window *w, bool on_p)
 33500 {
 33501   while (w)
 33502     {
 33503       if (WINDOWP (w->contents))
 33504         update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
 33505       else
 33506         update_window_cursor (w, on_p);
 33507 
 33508       w = NILP (w->next) ? 0 : XWINDOW (w->next);
 33509     }
 33510 }
 33511 
 33512 
 33513 /* EXPORT:
 33514    Display the cursor on window W, or clear it, according to ON_P.
 33515    Don't change the cursor's position.  */
 33516 
 33517 void
 33518 gui_update_cursor (struct frame *f, bool on_p)
 33519 {
 33520   update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
 33521 }
 33522 
 33523 
 33524 /* EXPORT:
 33525    Clear the cursor of window W to background color, and mark the
 33526    cursor as not shown.  This is used when the text where the cursor
 33527    is about to be rewritten.  */
 33528 
 33529 void
 33530 gui_clear_cursor (struct window *w)
 33531 {
 33532   if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
 33533     update_window_cursor (w, false);
 33534 }
 33535 
 33536 #endif /* HAVE_WINDOW_SYSTEM */
 33537 
 33538 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
 33539    and MSDOS.  */
 33540 static void
 33541 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
 33542                           int start_hpos, int end_hpos,
 33543                           enum draw_glyphs_face draw)
 33544 {
 33545 #ifdef HAVE_WINDOW_SYSTEM
 33546   if (FRAME_WINDOW_P (XFRAME (w->frame)))
 33547     {
 33548       draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
 33549       return;
 33550     }
 33551 #endif
 33552 
 33553   tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
 33554 }
 33555 
 33556 /* Display the active region described by mouse_face_* according to DRAW.  */
 33557 
 33558 static void
 33559 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
 33560 {
 33561   /* Don't bother doing anything if the mouse-face window is not set
 33562      up.  */
 33563   if (!WINDOWP (hlinfo->mouse_face_window))
 33564     return;
 33565 
 33566   struct window *w = XWINDOW (hlinfo->mouse_face_window);
 33567   struct frame *f = XFRAME (WINDOW_FRAME (w));
 33568 
 33569   /* Don't bother doing anything if we are on a wrong frame.  */
 33570   if (f != hlinfo->mouse_face_mouse_frame)
 33571     return;
 33572 
 33573   if (/* If window is in the process of being destroyed, don't bother
 33574          to do anything.  */
 33575       w->current_matrix != NULL
 33576       /* Don't update mouse highlight if hidden.  */
 33577       && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
 33578       /* Recognize when we are called to operate on rows that don't exist
 33579          anymore.  This can happen when a window is split.  */
 33580       && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
 33581     {
 33582       bool phys_cursor_on_p = w->phys_cursor_on_p;
 33583 #ifdef HAVE_WINDOW_SYSTEM
 33584       int mouse_off = 0;
 33585 #endif
 33586       struct glyph_row *row, *first, *last;
 33587 
 33588       first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
 33589       last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
 33590 
 33591       for (row = first; row <= last && row->enabled_p; ++row)
 33592         {
 33593           int start_hpos, end_hpos, start_x;
 33594 
 33595           /* For all but the first row, the highlight starts at column 0.  */
 33596           if (row == first)
 33597             {
 33598               /* R2L rows have BEG and END in reversed order, but the
 33599                  screen drawing geometry is always left to right.  So
 33600                  we need to mirror the beginning and end of the
 33601                  highlighted area in R2L rows.  */
 33602               if (!row->reversed_p)
 33603                 {
 33604                   start_hpos = hlinfo->mouse_face_beg_col;
 33605                   start_x = hlinfo->mouse_face_beg_x;
 33606                 }
 33607               else if (row == last)
 33608                 {
 33609                   start_hpos = hlinfo->mouse_face_end_col;
 33610                   start_x = hlinfo->mouse_face_end_x;
 33611                 }
 33612               else
 33613                 {
 33614                   start_hpos = 0;
 33615                   start_x = 0;
 33616                 }
 33617             }
 33618           else if (row->reversed_p && row == last)
 33619             {
 33620               start_hpos = hlinfo->mouse_face_end_col;
 33621               start_x = hlinfo->mouse_face_end_x;
 33622             }
 33623           else
 33624             {
 33625               start_hpos = 0;
 33626               start_x = 0;
 33627             }
 33628 
 33629           if (row == last)
 33630             {
 33631               if (!row->reversed_p)
 33632                 end_hpos = hlinfo->mouse_face_end_col;
 33633               else if (row == first)
 33634                 end_hpos = hlinfo->mouse_face_beg_col;
 33635               else
 33636                 {
 33637                   end_hpos = row->used[TEXT_AREA];
 33638                   if (draw == DRAW_NORMAL_TEXT)
 33639                     row->fill_line_p = true; /* Clear to end of line.  */
 33640                 }
 33641             }
 33642           else if (row->reversed_p && row == first)
 33643             end_hpos = hlinfo->mouse_face_beg_col;
 33644           else
 33645             {
 33646               end_hpos = row->used[TEXT_AREA];
 33647               if (draw == DRAW_NORMAL_TEXT)
 33648                 row->fill_line_p = true; /* Clear to end of line.  */
 33649             }
 33650 
 33651           if (end_hpos > start_hpos)
 33652             {
 33653               draw_row_with_mouse_face (w, start_x, row,
 33654                                         start_hpos, end_hpos, draw);
 33655 
 33656               row->mouse_face_p
 33657                 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
 33658             }
 33659 #ifdef HAVE_WINDOW_SYSTEM
 33660           /* Compute the cursor offset due to mouse-highlight.  */
 33661           if ((MATRIX_ROW_VPOS (row, w->current_matrix) == w->phys_cursor.vpos)
 33662               /* But not when highlighting a pseudo window, such as
 33663                  the toolbar, which can't have a cursor anyway.  */
 33664               && !w->pseudo_window_p
 33665               && draw == DRAW_MOUSE_FACE)
 33666             get_cursor_offset_for_mouse_face (w, row, &mouse_off);
 33667 #endif
 33668         }
 33669 
 33670       /* When we've written over the cursor, arrange for it to
 33671          be displayed again.  */
 33672       if (FRAME_WINDOW_P (f)
 33673           && phys_cursor_on_p && !w->phys_cursor_on_p)
 33674         {
 33675 #ifdef HAVE_WINDOW_SYSTEM
 33676           int hpos = w->phys_cursor.hpos;
 33677           int old_phys_cursor_x = w->phys_cursor.x;
 33678 
 33679           /* When the window is hscrolled, cursor hpos can legitimately be
 33680              out of bounds, but we draw the cursor at the corresponding
 33681              window margin in that case.  */
 33682           if (!row->reversed_p && hpos < 0)
 33683             hpos = 0;
 33684           if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33685             hpos = row->used[TEXT_AREA] - 1;
 33686 
 33687           block_input ();
 33688           display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
 33689                                   w->phys_cursor.x + mouse_off,
 33690                                   w->phys_cursor.y);
 33691           /* Restore the original cursor coordinates, perhaps modified
 33692              to account for mouse-highlight.  */
 33693           w->phys_cursor.x = old_phys_cursor_x;
 33694           unblock_input ();
 33695 #endif  /* HAVE_WINDOW_SYSTEM */
 33696         }
 33697     }
 33698 
 33699 #ifdef HAVE_WINDOW_SYSTEM
 33700   /* Change the mouse cursor.  */
 33701   if (FRAME_WINDOW_P (f) && NILP (track_mouse))
 33702     {
 33703       if (draw == DRAW_NORMAL_TEXT
 33704 #ifndef HAVE_EXT_TOOL_BAR
 33705           && !EQ (hlinfo->mouse_face_window, f->tool_bar_window)
 33706 #endif
 33707           && !EQ (hlinfo->mouse_face_window, f->tab_bar_window))
 33708         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->text_cursor);
 33709       else
 33710       if (draw == DRAW_MOUSE_FACE)
 33711         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->hand_cursor);
 33712       else
 33713         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->nontext_cursor);
 33714     }
 33715 #endif  /* HAVE_WINDOW_SYSTEM */
 33716 }
 33717 
 33718 /* EXPORT:
 33719    Clear out the mouse-highlighted active region.
 33720    Redraw it un-highlighted first.  Value is true if mouse
 33721    face was actually drawn unhighlighted.  */
 33722 
 33723 bool
 33724 clear_mouse_face (Mouse_HLInfo *hlinfo)
 33725 {
 33726   bool cleared
 33727     = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
 33728   if (cleared)
 33729     show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
 33730   hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
 33731   hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
 33732   hlinfo->mouse_face_window = Qnil;
 33733   hlinfo->mouse_face_overlay = Qnil;
 33734   return cleared;
 33735 }
 33736 
 33737 /* Return true if the coordinates HPOS and VPOS on windows W are
 33738    within the mouse face on that window.  */
 33739 static bool
 33740 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
 33741 {
 33742   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 33743 
 33744   /* Quickly resolve the easy cases.  */
 33745   if (!(WINDOWP (hlinfo->mouse_face_window)
 33746         && XWINDOW (hlinfo->mouse_face_window) == w))
 33747     return false;
 33748   if (vpos < hlinfo->mouse_face_beg_row
 33749       || vpos > hlinfo->mouse_face_end_row)
 33750     return false;
 33751   if (vpos > hlinfo->mouse_face_beg_row
 33752       && vpos < hlinfo->mouse_face_end_row)
 33753     return true;
 33754 
 33755   if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
 33756     {
 33757       if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 33758         {
 33759           if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
 33760             return true;
 33761         }
 33762       else if ((vpos == hlinfo->mouse_face_beg_row
 33763                 && hpos >= hlinfo->mouse_face_beg_col)
 33764                || (vpos == hlinfo->mouse_face_end_row
 33765                    && hpos < hlinfo->mouse_face_end_col))
 33766         return true;
 33767     }
 33768   else
 33769     {
 33770        if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 33771         {
 33772           if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
 33773             return true;
 33774         }
 33775       else if ((vpos == hlinfo->mouse_face_beg_row
 33776                 && hpos <= hlinfo->mouse_face_beg_col)
 33777                || (vpos == hlinfo->mouse_face_end_row
 33778                    && hpos > hlinfo->mouse_face_end_col))
 33779         return true;
 33780     }
 33781   return false;
 33782 }
 33783 
 33784 
 33785 /* EXPORT:
 33786    True if physical cursor of window W is within mouse face.  */
 33787 
 33788 bool
 33789 cursor_in_mouse_face_p (struct window *w)
 33790 {
 33791   int vpos = w->phys_cursor.vpos;
 33792 
 33793   /* If the cursor is outside the matrix glyph rows, it cannot be
 33794      within the mouse face.  */
 33795   if (!(0 <= vpos && vpos < w->current_matrix->nrows))
 33796     return false;
 33797 
 33798   int hpos = w->phys_cursor.hpos;
 33799   struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
 33800 
 33801   /* When the window is hscrolled, cursor hpos can legitimately be out
 33802      of bounds, but we draw the cursor at the corresponding window
 33803      margin in that case.  */
 33804   if (!row->reversed_p && hpos < 0)
 33805     hpos = 0;
 33806   if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33807     hpos = row->used[TEXT_AREA] - 1;
 33808 
 33809   return coords_in_mouse_face_p (w, hpos, vpos);
 33810 }
 33811 
 33812 
 33813 
 33814 /* Find the glyph rows START_ROW and END_ROW of window W that display
 33815    characters between buffer positions START_CHARPOS and END_CHARPOS
 33816    (excluding END_CHARPOS).  DISP_STRING is a display string that
 33817    covers these buffer positions.  This is similar to
 33818    row_containing_pos, but is more accurate when bidi reordering makes
 33819    buffer positions change non-linearly with glyph rows.  */
 33820 static void
 33821 rows_from_pos_range (struct window *w,
 33822                      ptrdiff_t start_charpos, ptrdiff_t end_charpos,
 33823                      Lisp_Object disp_string,
 33824                      struct glyph_row **start, struct glyph_row **end)
 33825 {
 33826   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 33827   int last_y = window_text_bottom_y (w);
 33828   struct glyph_row *row;
 33829 
 33830   *start = NULL;
 33831   *end = NULL;
 33832 
 33833   while (!first->enabled_p
 33834          && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
 33835     first++;
 33836 
 33837   /* Find the START row.  */
 33838   for (row = first;
 33839        row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
 33840        row++)
 33841     {
 33842       /* A row can potentially be the START row if the range of the
 33843          characters it displays intersects the range
 33844          [START_CHARPOS..END_CHARPOS).  */
 33845       if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
 33846               && end_charpos < MATRIX_ROW_START_CHARPOS (row))
 33847              /* See the commentary in row_containing_pos, for the
 33848                 explanation of the complicated way to check whether
 33849                 some position is beyond the end of the characters
 33850                 displayed by a row.  */
 33851              || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
 33852                   || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
 33853                       && !row->ends_at_zv_p
 33854                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 33855                  && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
 33856                      || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
 33857                          && !row->ends_at_zv_p
 33858                          && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
 33859         {
 33860           /* Found a candidate row.  Now make sure at least one of the
 33861              glyphs it displays has a charpos from the range
 33862              [START_CHARPOS..END_CHARPOS).
 33863 
 33864              This is not obvious because bidi reordering could make
 33865              buffer positions of a row be 1,2,3,102,101,100, and if we
 33866              want to highlight characters in [50..60), we don't want
 33867              this row, even though [50..60) does intersect [1..103),
 33868              the range of character positions given by the row's start
 33869              and end positions.  */
 33870           struct glyph *g = row->glyphs[TEXT_AREA];
 33871           struct glyph *e = g + row->used[TEXT_AREA];
 33872 
 33873           while (g < e)
 33874             {
 33875               if (((BUFFERP (g->object) || NILP (g->object))
 33876                    && start_charpos <= g->charpos && g->charpos < end_charpos)
 33877                   /* A glyph that comes from DISP_STRING is by
 33878                      definition to be highlighted.  */
 33879                   || EQ (g->object, disp_string))
 33880                 *start = row;
 33881               g++;
 33882             }
 33883           if (*start)
 33884             break;
 33885         }
 33886     }
 33887 
 33888   /* Find the END row.  */
 33889   if (!*start
 33890       /* If the last row is partially visible, start looking for END
 33891          from that row, instead of starting from FIRST.  */
 33892       && !(row->enabled_p
 33893            && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
 33894     row = first;
 33895   for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
 33896     {
 33897       struct glyph_row *next = row + 1;
 33898       ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
 33899 
 33900       if (!next->enabled_p
 33901           || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
 33902           /* The first row >= START whose range of displayed characters
 33903              does NOT intersect the range [START_CHARPOS..END_CHARPOS]
 33904              is the row END + 1.  */
 33905           || (start_charpos < next_start
 33906               && end_charpos < next_start)
 33907           || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
 33908                || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
 33909                    && !next->ends_at_zv_p
 33910                    && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
 33911               && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
 33912                   || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
 33913                       && !next->ends_at_zv_p
 33914                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
 33915         {
 33916           *end = row;
 33917           break;
 33918         }
 33919       else
 33920         {
 33921           /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
 33922              but none of the characters it displays are in the range, it is
 33923              also END + 1. */
 33924           struct glyph *g = next->glyphs[TEXT_AREA];
 33925           struct glyph *s = g;
 33926           struct glyph *e = g + next->used[TEXT_AREA];
 33927 
 33928           while (g < e)
 33929             {
 33930               if (((BUFFERP (g->object) || NILP (g->object))
 33931                    && ((start_charpos <= g->charpos && g->charpos < end_charpos)
 33932                        /* If the buffer position of the first glyph in
 33933                           the row is equal to END_CHARPOS, it means
 33934                           the last character to be highlighted is the
 33935                           newline of ROW, and we must consider NEXT as
 33936                           END, not END+1.  */
 33937                        || (((!next->reversed_p && g == s)
 33938                             || (next->reversed_p && g == e - 1))
 33939                            && (g->charpos == end_charpos
 33940                                /* Special case for when NEXT is an
 33941                                   empty line at ZV.  */
 33942                                || (g->charpos == -1
 33943                                    && !row->ends_at_zv_p
 33944                                    && next_start == end_charpos)))))
 33945                   /* A glyph that comes from DISP_STRING is by
 33946                      definition to be highlighted.  */
 33947                   || EQ (g->object, disp_string))
 33948                 break;
 33949               g++;
 33950             }
 33951           if (g == e)
 33952             {
 33953               *end = row;
 33954               break;
 33955             }
 33956           /* The first row that ends at ZV must be the last to be
 33957              highlighted.  */
 33958           else if (next->ends_at_zv_p)
 33959             {
 33960               *end = next;
 33961               break;
 33962             }
 33963         }
 33964     }
 33965 }
 33966 
 33967 /* This function sets the mouse_face_* elements of HLINFO, assuming
 33968    the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
 33969    window WINDOW.  START_CHARPOS and END_CHARPOS are buffer positions
 33970    for the overlay or run of text properties specifying the mouse
 33971    face.  BEFORE_STRING and AFTER_STRING, if non-nil, are a
 33972    before-string and after-string that must also be highlighted.
 33973    DISP_STRING, if non-nil, is a display string that may cover some
 33974    or all of the highlighted text.  */
 33975 
 33976 static void
 33977 mouse_face_from_buffer_pos (Lisp_Object window,
 33978                             Mouse_HLInfo *hlinfo,
 33979                             ptrdiff_t mouse_charpos,
 33980                             ptrdiff_t start_charpos,
 33981                             ptrdiff_t end_charpos,
 33982                             Lisp_Object before_string,
 33983                             Lisp_Object after_string,
 33984                             Lisp_Object disp_string)
 33985 {
 33986   struct window *w = XWINDOW (window);
 33987   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 33988   struct glyph_row *r1, *r2;
 33989   struct glyph *glyph, *end;
 33990   ptrdiff_t ignore, pos;
 33991   int x;
 33992 
 33993   eassert (NILP (disp_string) || STRINGP (disp_string));
 33994   eassert (NILP (before_string) || STRINGP (before_string));
 33995   eassert (NILP (after_string) || STRINGP (after_string));
 33996 
 33997   /* Find the rows corresponding to START_CHARPOS and END_CHARPOS.  */
 33998   rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
 33999   if (r1 == NULL)
 34000     r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34001   /* If the before-string or display-string contains newlines,
 34002      rows_from_pos_range skips to its last row.  Move back.  */
 34003   if (!NILP (before_string) || !NILP (disp_string))
 34004     {
 34005       struct glyph_row *prev;
 34006       while ((prev = r1 - 1, prev >= first)
 34007              && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
 34008              && prev->used[TEXT_AREA] > 0)
 34009         {
 34010           struct glyph *beg = prev->glyphs[TEXT_AREA];
 34011           glyph = beg + prev->used[TEXT_AREA];
 34012           while (--glyph >= beg && NILP (glyph->object));
 34013           if (glyph < beg
 34014               || !(EQ (glyph->object, before_string)
 34015                    || EQ (glyph->object, disp_string)))
 34016             break;
 34017           r1 = prev;
 34018         }
 34019     }
 34020   if (r2 == NULL)
 34021     {
 34022       r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34023       hlinfo->mouse_face_past_end = true;
 34024     }
 34025   else if (!NILP (after_string))
 34026     {
 34027       /* If the after-string has newlines, advance to its last row.  */
 34028       struct glyph_row *next;
 34029       struct glyph_row *last
 34030         = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34031 
 34032       for (next = r2 + 1;
 34033            next <= last
 34034              && next->used[TEXT_AREA] > 0
 34035              && EQ (next->glyphs[TEXT_AREA]->object, after_string);
 34036            ++next)
 34037         r2 = next;
 34038     }
 34039   /* The rest of the display engine assumes that mouse_face_beg_row is
 34040      either above mouse_face_end_row or identical to it.  But with
 34041      bidi-reordered continued lines, the row for START_CHARPOS could
 34042      be below the row for END_CHARPOS.  If so, swap the rows and store
 34043      them in correct order.  */
 34044   if (r1->y > r2->y)
 34045     {
 34046       struct glyph_row *tem = r2;
 34047 
 34048       r2 = r1;
 34049       r1 = tem;
 34050     }
 34051 
 34052   hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
 34053   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
 34054 
 34055   /* For a bidi-reordered row, the positions of BEFORE_STRING,
 34056      AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
 34057      could be anywhere in the row and in any order.  The strategy
 34058      below is to find the leftmost and the rightmost glyph that
 34059      belongs to either of these 3 strings, or whose position is
 34060      between START_CHARPOS and END_CHARPOS, and highlight all the
 34061      glyphs between those two.  This may cover more than just the text
 34062      between START_CHARPOS and END_CHARPOS if the range of characters
 34063      strides the bidi level boundary, e.g. if the beginning is in R2L
 34064      text while the end is in L2R text or vice versa.  */
 34065   if (!r1->reversed_p)
 34066     {
 34067       /* This row is in a left to right paragraph.  Scan it left to
 34068          right.  */
 34069       glyph = r1->glyphs[TEXT_AREA];
 34070       end = glyph + r1->used[TEXT_AREA];
 34071       x = r1->x;
 34072 
 34073       /* Skip truncation glyphs at the start of the glyph row.  */
 34074       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34075         for (; glyph < end
 34076                && NILP (glyph->object)
 34077                && glyph->charpos < 0;
 34078              ++glyph)
 34079           x += glyph->pixel_width;
 34080 
 34081       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34082          or DISP_STRING, and the first glyph from buffer whose
 34083          position is between START_CHARPOS and END_CHARPOS.  */
 34084       for (; glyph < end
 34085              && !NILP (glyph->object)
 34086              && !EQ (glyph->object, disp_string)
 34087              && !(BUFFERP (glyph->object)
 34088                   && (glyph->charpos >= start_charpos
 34089                       && glyph->charpos < end_charpos));
 34090            ++glyph)
 34091         {
 34092           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34093              are present at buffer positions between START_CHARPOS and
 34094              END_CHARPOS, or if they come from an overlay.  */
 34095           if (EQ (glyph->object, before_string))
 34096             {
 34097               pos = string_buffer_position (before_string,
 34098                                             start_charpos);
 34099               /* If pos == 0, it means before_string came from an
 34100                  overlay, not from a buffer position.  */
 34101               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34102                 break;
 34103             }
 34104           else if (EQ (glyph->object, after_string))
 34105             {
 34106               pos = string_buffer_position (after_string, end_charpos);
 34107               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34108                 break;
 34109             }
 34110           x += glyph->pixel_width;
 34111         }
 34112       hlinfo->mouse_face_beg_x = x;
 34113       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34114     }
 34115   else
 34116     {
 34117       /* This row is in a right to left paragraph.  Scan it right to
 34118          left.  */
 34119       struct glyph *g;
 34120 
 34121       end = r1->glyphs[TEXT_AREA] - 1;
 34122       glyph = end + r1->used[TEXT_AREA];
 34123 
 34124       /* Skip truncation glyphs at the start of the glyph row.  */
 34125       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34126         for (; glyph > end
 34127                && NILP (glyph->object)
 34128                && glyph->charpos < 0;
 34129              --glyph)
 34130           ;
 34131 
 34132       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34133          or DISP_STRING, and the first glyph from buffer whose
 34134          position is between START_CHARPOS and END_CHARPOS.  */
 34135       for (; glyph > end
 34136              && !NILP (glyph->object)
 34137              && !EQ (glyph->object, disp_string)
 34138              && !(BUFFERP (glyph->object)
 34139                   && (glyph->charpos >= start_charpos
 34140                       && glyph->charpos < end_charpos));
 34141            --glyph)
 34142         {
 34143           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34144              are present at buffer positions between START_CHARPOS and
 34145              END_CHARPOS, or if they come from an overlay.  */
 34146           if (EQ (glyph->object, before_string))
 34147             {
 34148               pos = string_buffer_position (before_string, start_charpos);
 34149               /* If pos == 0, it means before_string came from an
 34150                  overlay, not from a buffer position.  */
 34151               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34152                 break;
 34153             }
 34154           else if (EQ (glyph->object, after_string))
 34155             {
 34156               pos = string_buffer_position (after_string, end_charpos);
 34157               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34158                 break;
 34159             }
 34160         }
 34161 
 34162       glyph++; /* first glyph to the right of the highlighted area */
 34163       for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
 34164         x += g->pixel_width;
 34165       hlinfo->mouse_face_beg_x = x;
 34166       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34167     }
 34168 
 34169   /* If the highlight ends in a different row, compute GLYPH and END
 34170      for the end row.  Otherwise, reuse the values computed above for
 34171      the row where the highlight begins.  */
 34172   if (r2 != r1)
 34173     {
 34174       if (!r2->reversed_p)
 34175         {
 34176           glyph = r2->glyphs[TEXT_AREA];
 34177           end = glyph + r2->used[TEXT_AREA];
 34178           x = r2->x;
 34179         }
 34180       else
 34181         {
 34182           end = r2->glyphs[TEXT_AREA] - 1;
 34183           glyph = end + r2->used[TEXT_AREA];
 34184         }
 34185     }
 34186 
 34187   if (!r2->reversed_p)
 34188     {
 34189       /* Skip truncation and continuation glyphs near the end of the
 34190          row, and also blanks and stretch glyphs inserted by
 34191          extend_face_to_end_of_line.  */
 34192       while (end > glyph
 34193              && NILP ((end - 1)->object))
 34194         --end;
 34195       /* Scan the rest of the glyph row from the end, looking for the
 34196          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34197          DISP_STRING, or whose position is between START_CHARPOS
 34198          and END_CHARPOS */
 34199       for (--end;
 34200              end > glyph
 34201              && !NILP (end->object)
 34202              && !EQ (end->object, disp_string)
 34203              && !(BUFFERP (end->object)
 34204                   && (end->charpos >= start_charpos
 34205                       && end->charpos < end_charpos));
 34206            --end)
 34207         {
 34208           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34209              are present at buffer positions between START_CHARPOS and
 34210              END_CHARPOS, or if they come from an overlay.  */
 34211           if (EQ (end->object, before_string))
 34212             {
 34213               pos = string_buffer_position (before_string, start_charpos);
 34214               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34215                 break;
 34216             }
 34217           else if (EQ (end->object, after_string))
 34218             {
 34219               pos = string_buffer_position (after_string, end_charpos);
 34220               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34221                 break;
 34222             }
 34223         }
 34224       /* Find the X coordinate of the last glyph to be highlighted.  */
 34225       for (; glyph <= end; ++glyph)
 34226         x += glyph->pixel_width;
 34227 
 34228       hlinfo->mouse_face_end_x = x;
 34229       hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
 34230     }
 34231   else
 34232     {
 34233       /* Skip truncation and continuation glyphs near the end of the
 34234          row, and also blanks and stretch glyphs inserted by
 34235          extend_face_to_end_of_line.  */
 34236       x = r2->x;
 34237       end++;
 34238       while (end < glyph
 34239              && NILP (end->object))
 34240         {
 34241           x += end->pixel_width;
 34242           ++end;
 34243         }
 34244       /* Scan the rest of the glyph row from the end, looking for the
 34245          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34246          DISP_STRING, or whose position is between START_CHARPOS
 34247          and END_CHARPOS */
 34248       for ( ;
 34249              end < glyph
 34250              && !NILP (end->object)
 34251              && !EQ (end->object, disp_string)
 34252              && !(BUFFERP (end->object)
 34253                   && (end->charpos >= start_charpos
 34254                       && end->charpos < end_charpos));
 34255            ++end)
 34256         {
 34257           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34258              are present at buffer positions between START_CHARPOS and
 34259              END_CHARPOS, or if they come from an overlay.  */
 34260           if (EQ (end->object, before_string))
 34261             {
 34262               pos = string_buffer_position (before_string, start_charpos);
 34263               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34264                 break;
 34265             }
 34266           else if (EQ (end->object, after_string))
 34267             {
 34268               pos = string_buffer_position (after_string, end_charpos);
 34269               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34270                 break;
 34271             }
 34272           x += end->pixel_width;
 34273         }
 34274       /* If we exited the above loop because we arrived at the last
 34275          glyph of the row, and its buffer position is still not in
 34276          range, it means the last character in range is the preceding
 34277          newline.  Bump the end column and x values to get past the
 34278          last glyph.  */
 34279       if (end == glyph
 34280           && BUFFERP (end->object)
 34281           && (end->charpos < start_charpos
 34282               || end->charpos >= end_charpos))
 34283         {
 34284           x += end->pixel_width;
 34285           ++end;
 34286         }
 34287       hlinfo->mouse_face_end_x = x;
 34288       hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
 34289     }
 34290 
 34291   hlinfo->mouse_face_window = window;
 34292   hlinfo->mouse_face_face_id
 34293     = face_at_buffer_position (w, mouse_charpos, &ignore,
 34294                                mouse_charpos + 1,
 34295                                !hlinfo->mouse_face_hidden, -1, 0);
 34296   show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 34297 }
 34298 
 34299 /* The following function is not used anymore (replaced with
 34300    mouse_face_from_string_pos), but I leave it here for the time
 34301    being, in case someone would.  */
 34302 
 34303 #if false       /* not used */
 34304 
 34305 /* Find the position of the glyph for position POS in OBJECT in
 34306    window W's current matrix, and return in *X, *Y the pixel
 34307    coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
 34308 
 34309    RIGHT_P means return the position of the right edge of the glyph.
 34310    !RIGHT_P means return the left edge position.
 34311 
 34312    If no glyph for POS exists in the matrix, return the position of
 34313    the glyph with the next smaller position that is in the matrix, if
 34314    RIGHT_P is false.  If RIGHT_P, and no glyph for POS
 34315    exists in the matrix, return the position of the glyph with the
 34316    next larger position in OBJECT.
 34317 
 34318    Value is true if a glyph was found.  */
 34319 
 34320 static bool
 34321 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
 34322                       int *hpos, int *vpos, int *x, int *y, bool right_p)
 34323 {
 34324   int yb = window_text_bottom_y (w);
 34325   struct glyph_row *r;
 34326   struct glyph *best_glyph = NULL;
 34327   struct glyph_row *best_row = NULL;
 34328   int best_x = 0;
 34329 
 34330   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34331        r->enabled_p && r->y < yb;
 34332        ++r)
 34333     {
 34334       struct glyph *g = r->glyphs[TEXT_AREA];
 34335       struct glyph *e = g + r->used[TEXT_AREA];
 34336       int gx;
 34337 
 34338       for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34339         if (EQ (g->object, object))
 34340           {
 34341             if (g->charpos == pos)
 34342               {
 34343                 best_glyph = g;
 34344                 best_x = gx;
 34345                 best_row = r;
 34346                 goto found;
 34347               }
 34348             else if (best_glyph == NULL
 34349                      || ((eabs (g->charpos - pos)
 34350                          < eabs (best_glyph->charpos - pos))
 34351                          && (right_p
 34352                              ? g->charpos < pos
 34353                              : g->charpos > pos)))
 34354               {
 34355                 best_glyph = g;
 34356                 best_x = gx;
 34357                 best_row = r;
 34358               }
 34359           }
 34360     }
 34361 
 34362  found:
 34363 
 34364   if (best_glyph)
 34365     {
 34366       *x = best_x;
 34367       *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
 34368 
 34369       if (right_p)
 34370         {
 34371           *x += best_glyph->pixel_width;
 34372           ++*hpos;
 34373         }
 34374 
 34375       *y = best_row->y;
 34376       *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
 34377     }
 34378 
 34379   return best_glyph != NULL;
 34380 }
 34381 #endif  /* not used */
 34382 
 34383 /* Find the positions of the first and the last glyphs in window W's
 34384    current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
 34385    (assumed to be a string), and return in HLINFO's mouse_face_*
 34386    members the pixel and column/row coordinates of those glyphs.  */
 34387 
 34388 static void
 34389 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
 34390                             Lisp_Object object,
 34391                             ptrdiff_t startpos, ptrdiff_t endpos)
 34392 {
 34393   int yb = window_text_bottom_y (w);
 34394   struct glyph_row *r;
 34395   struct glyph *g, *e;
 34396   int gx;
 34397   bool found = false;
 34398 
 34399   /* Find the glyph row with at least one position in the range
 34400      [STARTPOS..ENDPOS), and the first glyph in that row whose
 34401      position belongs to that range.  */
 34402   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34403        r->enabled_p && r->y < yb;
 34404        ++r)
 34405     {
 34406       if (!r->reversed_p)
 34407         {
 34408           g = r->glyphs[TEXT_AREA];
 34409           e = g + r->used[TEXT_AREA];
 34410           for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34411             if (EQ (g->object, object)
 34412                 && startpos <= g->charpos && g->charpos < endpos)
 34413               {
 34414                 hlinfo->mouse_face_beg_row
 34415                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34416                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34417                 hlinfo->mouse_face_beg_x = gx;
 34418                 found = true;
 34419                 break;
 34420               }
 34421         }
 34422       else
 34423         {
 34424           struct glyph *g1;
 34425 
 34426           e = r->glyphs[TEXT_AREA];
 34427           g = e + r->used[TEXT_AREA];
 34428           for ( ; g > e; --g)
 34429             if (EQ ((g-1)->object, object)
 34430                 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
 34431               {
 34432                 hlinfo->mouse_face_beg_row
 34433                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34434                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34435                 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
 34436                   gx += g1->pixel_width;
 34437                 hlinfo->mouse_face_beg_x = gx;
 34438                 found = true;
 34439                 break;
 34440               }
 34441         }
 34442       if (found)
 34443         break;
 34444     }
 34445 
 34446   if (!found)
 34447     return;
 34448 
 34449   /* Starting with the next row, look for the first row which does NOT
 34450      include any glyphs whose positions are in the range.  */
 34451   for (++r; r->enabled_p && r->y < yb; ++r)
 34452     {
 34453       g = r->glyphs[TEXT_AREA];
 34454       e = g + r->used[TEXT_AREA];
 34455       found = false;
 34456       for ( ; g < e; ++g)
 34457         if (EQ (g->object, object)
 34458             && startpos <= g->charpos && g->charpos < endpos)
 34459           {
 34460             found = true;
 34461             break;
 34462           }
 34463       if (!found)
 34464         break;
 34465     }
 34466 
 34467   /* The highlighted region ends on the previous row.  */
 34468   r--;
 34469 
 34470   /* Set the end row.  */
 34471   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
 34472 
 34473   /* Compute and set the end column and the end column's horizontal
 34474      pixel coordinate.  */
 34475   if (!r->reversed_p)
 34476     {
 34477       g = r->glyphs[TEXT_AREA];
 34478       e = g + r->used[TEXT_AREA];
 34479       for ( ; e > g; --e)
 34480         if (EQ ((e-1)->object, object)
 34481             && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
 34482           break;
 34483       hlinfo->mouse_face_end_col = e - g;
 34484 
 34485       for (gx = r->x; g < e; ++g)
 34486         gx += g->pixel_width;
 34487       hlinfo->mouse_face_end_x = gx;
 34488     }
 34489   else
 34490     {
 34491       e = r->glyphs[TEXT_AREA];
 34492       g = e + r->used[TEXT_AREA];
 34493       for (gx = r->x ; e < g; ++e)
 34494         {
 34495           if (EQ (e->object, object)
 34496               && startpos <= e->charpos && e->charpos < endpos)
 34497             break;
 34498           gx += e->pixel_width;
 34499         }
 34500       hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
 34501       hlinfo->mouse_face_end_x = gx;
 34502     }
 34503 }
 34504 
 34505 #ifdef HAVE_WINDOW_SYSTEM
 34506 
 34507 /* See if position X, Y is within a hot-spot of an image.  */
 34508 
 34509 static bool
 34510 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
 34511 {
 34512   if (!CONSP (hot_spot))
 34513     return false;
 34514 
 34515   if (EQ (XCAR (hot_spot), Qrect))
 34516     {
 34517       /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1))  */
 34518       Lisp_Object rect = XCDR (hot_spot);
 34519       Lisp_Object tem;
 34520       if (!CONSP (rect))
 34521         return false;
 34522       if (!CONSP (XCAR (rect)))
 34523         return false;
 34524       if (!CONSP (XCDR (rect)))
 34525         return false;
 34526       if (!(tem = XCAR (XCAR (rect)), FIXNUMP (tem) && x >= XFIXNUM (tem)))
 34527         return false;
 34528       if (!(tem = XCDR (XCAR (rect)), FIXNUMP (tem) && y >= XFIXNUM (tem)))
 34529         return false;
 34530       if (!(tem = XCAR (XCDR (rect)), FIXNUMP (tem) && x <= XFIXNUM (tem)))
 34531         return false;
 34532       if (!(tem = XCDR (XCDR (rect)), FIXNUMP (tem) && y <= XFIXNUM (tem)))
 34533         return false;
 34534       return true;
 34535     }
 34536   else if (EQ (XCAR (hot_spot), Qcircle))
 34537     {
 34538       /* CDR is (Center . Radius) = ((x0 . y0) . r) */
 34539       Lisp_Object circ = XCDR (hot_spot);
 34540       Lisp_Object lr, lx0, ly0;
 34541       if (CONSP (circ)
 34542           && CONSP (XCAR (circ))
 34543           && (lr = XCDR (circ), NUMBERP (lr))
 34544           && (lx0 = XCAR (XCAR (circ)), FIXNUMP (lx0))
 34545           && (ly0 = XCDR (XCAR (circ)), FIXNUMP (ly0)))
 34546         {
 34547           double r = XFLOATINT (lr);
 34548           double dx = XFIXNUM (lx0) - x;
 34549           double dy = XFIXNUM (ly0) - y;
 34550           return (dx * dx + dy * dy <= r * r);
 34551         }
 34552     }
 34553   else if (EQ (XCAR (hot_spot), Qpoly))
 34554     {
 34555       /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
 34556       if (VECTORP (XCDR (hot_spot)))
 34557         {
 34558           struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
 34559           Lisp_Object *poly = v->contents;
 34560           ptrdiff_t n = v->header.size;
 34561           ptrdiff_t i;
 34562           bool inside = false;
 34563           Lisp_Object lx, ly;
 34564           int x0, y0;
 34565 
 34566           /* Need an even number of coordinates, and at least 3 edges.  */
 34567           if (n < 6 || n & 1)
 34568             return false;
 34569 
 34570           /* Count edge segments intersecting line from (X,Y) to (X,infinity).
 34571              If count is odd, we are inside polygon.  Pixels on edges
 34572              may or may not be included depending on actual geometry of the
 34573              polygon.  */
 34574           if ((lx = poly[n-2], !FIXNUMP (lx))
 34575               || (ly = poly[n-1], !FIXNUMP (lx)))
 34576             return false;
 34577           x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34578           for (i = 0; i < n; i += 2)
 34579             {
 34580               int x1 = x0, y1 = y0;
 34581               if ((lx = poly[i], !FIXNUMP (lx))
 34582                   || (ly = poly[i+1], !FIXNUMP (ly)))
 34583                 return false;
 34584               x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34585 
 34586               /* Does this segment cross the X line?  */
 34587               if (x0 >= x)
 34588                 {
 34589                   if (x1 >= x)
 34590                     continue;
 34591                 }
 34592               else if (x1 < x)
 34593                 continue;
 34594               if (y > y0 && y > y1)
 34595                 continue;
 34596               if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
 34597                 inside = !inside;
 34598             }
 34599           return inside;
 34600         }
 34601     }
 34602   return false;
 34603 }
 34604 
 34605 Lisp_Object
 34606 find_hot_spot (Lisp_Object map, int x, int y)
 34607 {
 34608   while (CONSP (map))
 34609     {
 34610       if (CONSP (XCAR (map))
 34611           && on_hot_spot_p (XCAR (XCAR (map)), x, y))
 34612         return XCAR (map);
 34613       map = XCDR (map);
 34614     }
 34615 
 34616   return Qnil;
 34617 }
 34618 
 34619 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
 34620        3, 3, 0,
 34621        doc: /* Lookup in image map MAP coordinates X and Y.
 34622 An image map is an alist where each element has the format (AREA ID PLIST).
 34623 An AREA is specified as either a rectangle, a circle, or a polygon:
 34624 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
 34625 pixel coordinates of the upper left and bottom right corners.
 34626 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
 34627 and the radius of the circle; r may be a float or integer.
 34628 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
 34629 vector describes one corner in the polygon.
 34630 Returns the alist element for the first matching AREA in MAP.  */)
 34631   (Lisp_Object map, Lisp_Object x, Lisp_Object y)
 34632 {
 34633   if (NILP (map))
 34634     return Qnil;
 34635 
 34636   CHECK_FIXNUM (x);
 34637   CHECK_FIXNUM (y);
 34638 
 34639   return find_hot_spot (map,
 34640                         clip_to_bounds (INT_MIN, XFIXNUM (x), INT_MAX),
 34641                         clip_to_bounds (INT_MIN, XFIXNUM (y), INT_MAX));
 34642 }
 34643 #endif  /* HAVE_WINDOW_SYSTEM */
 34644 
 34645 
 34646 /* Display frame CURSOR, optionally using shape defined by POINTER.  */
 34647 static void
 34648 define_frame_cursor1 (struct frame *f, Emacs_Cursor cursor, Lisp_Object pointer)
 34649 {
 34650 #ifdef HAVE_WINDOW_SYSTEM
 34651   if (!FRAME_WINDOW_P (f))
 34652     return;
 34653 
 34654   /* Do not change cursor shape while dragging mouse.  */
 34655   if (EQ (track_mouse, Qdragging) || EQ (track_mouse, Qdropping)
 34656       || EQ (track_mouse, Qdrag_source))
 34657     return;
 34658 
 34659   if (!NILP (pointer))
 34660     {
 34661       if (EQ (pointer, Qarrow))
 34662         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34663       else if (EQ (pointer, Qhand))
 34664         cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 34665       else if (EQ (pointer, Qtext))
 34666         cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 34667       else if (EQ (pointer, intern ("hdrag")))
 34668         cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 34669       else if (EQ (pointer, intern ("nhdrag")))
 34670         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34671 # ifdef HAVE_X_WINDOWS
 34672       else if (EQ (pointer, intern ("vdrag")))
 34673         cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
 34674 # endif
 34675       else if (EQ (pointer, intern ("hourglass")))
 34676         cursor = FRAME_OUTPUT_DATA (f)->hourglass_cursor;
 34677       else if (EQ (pointer, Qmodeline))
 34678         cursor = FRAME_OUTPUT_DATA (f)->modeline_cursor;
 34679       else
 34680         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34681     }
 34682 
 34683   if (cursor != No_Cursor)
 34684     FRAME_RIF (f)->define_frame_cursor (f, cursor);
 34685 #endif
 34686 }
 34687 
 34688 /* Take proper action when mouse has moved to the mode or header line
 34689    or marginal area AREA of window W, x-position X and y-position Y.
 34690    X is relative to the start of the text display area of W, so the
 34691    width of bitmap areas and scroll bars must be subtracted to get a
 34692    position relative to the start of the mode line.  */
 34693 
 34694 static void
 34695 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
 34696                                     enum window_part area)
 34697 {
 34698   struct window *w = XWINDOW (window);
 34699   struct frame *f = XFRAME (w->frame);
 34700   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 34701   Emacs_Cursor cursor = No_Cursor;
 34702   Lisp_Object pointer = Qnil;
 34703   int dx, dy, width, height;
 34704   ptrdiff_t charpos;
 34705   Lisp_Object string, object = Qnil;
 34706   Lisp_Object pos UNINIT;
 34707   Lisp_Object mouse_face;
 34708   int original_x_pixel = x;
 34709   struct glyph * glyph = NULL, * row_start_glyph = NULL;
 34710   struct glyph_row *row UNINIT;
 34711 
 34712   if (area == ON_MODE_LINE || area == ON_HEADER_LINE || area == ON_TAB_LINE)
 34713     {
 34714       int x0;
 34715       struct glyph *end;
 34716 
 34717       /* Kludge alert: mode_line_string takes X/Y in pixels, but
 34718          returns them in row/column units!  */
 34719       string = mode_line_string (w, area, &x, &y, &charpos,
 34720                                  &object, &dx, &dy, &width, &height);
 34721 
 34722       row = (area == ON_MODE_LINE
 34723              ? MATRIX_MODE_LINE_ROW (w->current_matrix)
 34724              : (area == ON_TAB_LINE
 34725                 ? MATRIX_TAB_LINE_ROW (w->current_matrix)
 34726                 : MATRIX_HEADER_LINE_ROW (w->current_matrix)));
 34727 
 34728       /* Find the glyph under the mouse pointer.  */
 34729       if (row->mode_line_p && row->enabled_p)
 34730         {
 34731           glyph = row_start_glyph = row->glyphs[TEXT_AREA];
 34732           end = glyph + row->used[TEXT_AREA];
 34733 
 34734           for (x0 = original_x_pixel;
 34735                glyph < end && x0 >= glyph->pixel_width;
 34736                ++glyph)
 34737             x0 -= glyph->pixel_width;
 34738 
 34739           if (glyph >= end)
 34740             glyph = NULL;
 34741         }
 34742     }
 34743   else
 34744     {
 34745       x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
 34746       /* Kludge alert: marginal_area_string takes X/Y in pixels, but
 34747          returns them in row/column units!  */
 34748       string = marginal_area_string (w, area, &x, &y, &charpos,
 34749                                      &object, &dx, &dy, &width, &height);
 34750     }
 34751 
 34752   Lisp_Object help = Qnil;
 34753 
 34754 #ifdef HAVE_WINDOW_SYSTEM
 34755   if (IMAGEP (object))
 34756     {
 34757       Lisp_Object image_map, hotspot;
 34758       if ((image_map = plist_get (XCDR (object), QCmap),
 34759            !NILP (image_map))
 34760           && (hotspot = find_hot_spot (image_map, dx, dy),
 34761               CONSP (hotspot))
 34762           && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 34763         {
 34764           Lisp_Object plist;
 34765 
 34766           /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
 34767              If so, we could look for mouse-enter, mouse-leave
 34768              properties in PLIST (and do something...).  */
 34769           hotspot = XCDR (hotspot);
 34770           if (CONSP (hotspot)
 34771               && (plist = XCAR (hotspot), CONSP (plist)))
 34772             {
 34773               pointer = plist_get (plist, Qpointer);
 34774               if (NILP (pointer))
 34775                 pointer = Qhand;
 34776               help = plist_get (plist, Qhelp_echo);
 34777               if (!NILP (help))
 34778                 {
 34779                   help_echo_string = help;
 34780                   XSETWINDOW (help_echo_window, w);
 34781                   help_echo_object = w->contents;
 34782                   help_echo_pos = charpos;
 34783                 }
 34784             }
 34785         }
 34786       if (NILP (pointer))
 34787         pointer = plist_get (XCDR (object), QCpointer);
 34788     }
 34789 #endif  /* HAVE_WINDOW_SYSTEM */
 34790 
 34791   /* CHARPOS can be beyond the last position of STRING due, e.g., to
 34792      min-width 'display' property.  Fix that, to let all the calls to
 34793      get-text-property below do their thing.  */
 34794   if (STRINGP (string))
 34795     pos = make_fixnum (min (charpos, SCHARS (string) - 1));
 34796 
 34797   /* Set the help text and mouse pointer.  If the mouse is on a part
 34798      of the mode line without any text (e.g. past the right edge of
 34799      the mode line text), use that windows's mode line help echo if it
 34800      has been set.  */
 34801   if (STRINGP (string) || area == ON_MODE_LINE || area == ON_HEADER_LINE
 34802       || area == ON_TAB_LINE)
 34803     {
 34804       /* Arrange to display the help by setting the global variables
 34805          help_echo_string, help_echo_object, and help_echo_pos.  */
 34806       if (NILP (help))
 34807         {
 34808           if (STRINGP (string))
 34809             help = Fget_text_property (pos, Qhelp_echo, string);
 34810 
 34811           if (!NILP (help))
 34812             {
 34813               help_echo_string = help;
 34814               XSETWINDOW (help_echo_window, w);
 34815               help_echo_object = string;
 34816               help_echo_pos = charpos;
 34817             }
 34818           else if (area == ON_MODE_LINE
 34819                    && !NILP (w->mode_line_help_echo))
 34820             {
 34821               help_echo_string =  w->mode_line_help_echo;
 34822               XSETWINDOW (help_echo_window, w);
 34823               help_echo_object = Qnil;
 34824               help_echo_pos = -1;
 34825             }
 34826         }
 34827 
 34828 #ifdef HAVE_WINDOW_SYSTEM
 34829       /* Change the mouse pointer according to what is under it.  */
 34830       if (FRAME_WINDOW_P (f))
 34831         {
 34832           bool draggable = (! WINDOW_BOTTOMMOST_P (w)
 34833                             || minibuf_level
 34834                             || NILP (Vresize_mini_windows));
 34835 
 34836           if (STRINGP (string))
 34837             {
 34838               cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34839 
 34840               if (NILP (pointer))
 34841                 pointer = Fget_text_property (pos, Qpointer, string);
 34842 
 34843               /* Change the mouse pointer according to what is under X/Y.  */
 34844               if (NILP (pointer)
 34845                   && (area == ON_MODE_LINE || area == ON_HEADER_LINE
 34846                       || area == ON_TAB_LINE))
 34847                 {
 34848                   Lisp_Object map;
 34849 
 34850                   map = Fget_text_property (pos, Qlocal_map, string);
 34851                   if (!KEYMAPP (map))
 34852                     map = Fget_text_property (pos, Qkeymap, string);
 34853                   if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
 34854                     cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34855                 }
 34856             }
 34857           else if (draggable && area == ON_MODE_LINE)
 34858             cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34859           else if ((area == ON_MODE_LINE
 34860                     && WINDOW_BOTTOMMOST_P (w)
 34861                     && !FRAME_HAS_MINIBUF_P (f)
 34862                     && !NILP (Fframe_parameter
 34863                               (w->frame, Qdrag_with_mode_line)))
 34864                    || (((area == ON_HEADER_LINE
 34865                          && !NILP (Fframe_parameter
 34866                                    (w->frame, Qdrag_with_header_line)))
 34867                         || (area == ON_TAB_LINE
 34868                             && !NILP (Fframe_parameter
 34869                                       (w->frame, Qdrag_with_tab_line))))
 34870                        && WINDOW_TOPMOST_P (w)))
 34871             cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 34872           else
 34873             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34874         }
 34875 #endif
 34876     }
 34877 
 34878   /* Change the mouse face according to what is under X/Y.  */
 34879   bool mouse_face_shown = false;
 34880 
 34881   if (STRINGP (string))
 34882     {
 34883       mouse_face = Fget_text_property (pos, Qmouse_face, string);
 34884       if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
 34885           && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)
 34886               || (area == ON_TAB_LINE))
 34887           && glyph)
 34888         {
 34889           Lisp_Object b, e;
 34890 
 34891           struct glyph * tmp_glyph;
 34892 
 34893           int gpos;
 34894           int gseq_length;
 34895           int total_pixel_width;
 34896           ptrdiff_t begpos, endpos, ignore;
 34897 
 34898           int vpos, hpos;
 34899 
 34900           b = Fprevious_single_property_change (make_fixnum (charpos + 1),
 34901                                                 Qmouse_face, string, Qnil);
 34902           if (NILP (b))
 34903             begpos = 0;
 34904           else
 34905             begpos = XFIXNUM (b);
 34906 
 34907           e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
 34908           if (NILP (e))
 34909             endpos = SCHARS (string);
 34910           else
 34911             endpos = XFIXNUM (e);
 34912 
 34913           /* Calculate the glyph position GPOS of GLYPH in the
 34914              displayed string, relative to the beginning of the
 34915              highlighted part of the string.
 34916 
 34917              Note: GPOS is different from CHARPOS.  CHARPOS is the
 34918              position of GLYPH in the internal string object.  A mode
 34919              line string format has structures which are converted to
 34920              a flattened string by the Emacs Lisp interpreter.  The
 34921              internal string is an element of those structures.  The
 34922              displayed string is the flattened string.  */
 34923           tmp_glyph = row_start_glyph;
 34924           while (tmp_glyph < glyph
 34925                  && (!(EQ (tmp_glyph->object, glyph->object)
 34926                        && begpos <= tmp_glyph->charpos
 34927                        && tmp_glyph->charpos < endpos)))
 34928             tmp_glyph++;
 34929           gpos = glyph - tmp_glyph;
 34930 
 34931           /* Calculate the length GSEQ_LENGTH of the glyph sequence of
 34932              the highlighted part of the displayed string to which
 34933              GLYPH belongs.  Note: GSEQ_LENGTH is different from
 34934              SCHARS (STRING), because the latter returns the length of
 34935              the internal string.  */
 34936           for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 34937                tmp_glyph > glyph
 34938                  && (!(EQ (tmp_glyph->object, glyph->object)
 34939                        && begpos <= tmp_glyph->charpos
 34940                        && tmp_glyph->charpos < endpos));
 34941                tmp_glyph--)
 34942             ;
 34943           gseq_length = gpos + (tmp_glyph - glyph) + 1;
 34944 
 34945           /* Calculate the total pixel width of all the glyphs between
 34946              the beginning of the highlighted area and GLYPH.  */
 34947           total_pixel_width = 0;
 34948           for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
 34949             total_pixel_width += tmp_glyph->pixel_width;
 34950 
 34951           /* Pre calculation of re-rendering position.  Note: X is in
 34952              column units here, after the call to mode_line_string or
 34953              marginal_area_string.  */
 34954           hpos = x - gpos;
 34955           vpos = (area == ON_MODE_LINE
 34956                   ? (w->current_matrix)->nrows - 1
 34957                   : (area == ON_TAB_LINE
 34958                      ? 0
 34959                      : (w->current_matrix->tab_line_p
 34960                         ? 1
 34961                         : 0)));
 34962 
 34963           /* If GLYPH's position is included in the region that is
 34964              already drawn in mouse face, we have nothing to do.  */
 34965           if ( EQ (window, hlinfo->mouse_face_window)
 34966                && (!row->reversed_p
 34967                    ? (hlinfo->mouse_face_beg_col <= hpos
 34968                       && hpos < hlinfo->mouse_face_end_col)
 34969                    /* In R2L rows we swap BEG and END, see below.  */
 34970                    : (hlinfo->mouse_face_end_col <= hpos
 34971                       && hpos < hlinfo->mouse_face_beg_col))
 34972                && hlinfo->mouse_face_beg_row == vpos )
 34973             return;
 34974 
 34975           if (clear_mouse_face (hlinfo))
 34976             cursor = No_Cursor;
 34977 
 34978           if (!row->reversed_p)
 34979             {
 34980               hlinfo->mouse_face_beg_col = hpos;
 34981               hlinfo->mouse_face_beg_x   = original_x_pixel
 34982                                             - (total_pixel_width + dx);
 34983               hlinfo->mouse_face_end_col = hpos + gseq_length;
 34984               hlinfo->mouse_face_end_x   = 0;
 34985             }
 34986           else
 34987             {
 34988               /* In R2L rows, show_mouse_face expects BEG and END
 34989                  coordinates to be swapped.  */
 34990               hlinfo->mouse_face_end_col = hpos;
 34991               hlinfo->mouse_face_end_x   = original_x_pixel
 34992                                             - (total_pixel_width + dx);
 34993               hlinfo->mouse_face_beg_col = hpos + gseq_length;
 34994               hlinfo->mouse_face_beg_x   = 0;
 34995             }
 34996 
 34997           hlinfo->mouse_face_beg_row  = vpos;
 34998           hlinfo->mouse_face_end_row  = hlinfo->mouse_face_beg_row;
 34999           hlinfo->mouse_face_past_end = false;
 35000           hlinfo->mouse_face_window   = window;
 35001 
 35002           hlinfo->mouse_face_face_id =
 35003             face_at_string_position (w, string, charpos, 0, &ignore,
 35004                                      glyph->face_id, true, 0);
 35005 
 35006           show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35007           mouse_face_shown = true;
 35008 
 35009           if (NILP (pointer))
 35010             pointer = Qhand;
 35011         }
 35012     }
 35013 
 35014   /* If mouse-face doesn't need to be shown, clear any existing
 35015      mouse-face.  */
 35016   if ((area == ON_MODE_LINE || area == ON_HEADER_LINE
 35017        || area == ON_TAB_LINE) && !mouse_face_shown)
 35018     clear_mouse_face (hlinfo);
 35019 
 35020   define_frame_cursor1 (f, cursor, pointer);
 35021 }
 35022 
 35023 
 35024 /* EXPORT:
 35025    Take proper action when the mouse has moved to position X, Y on
 35026    frame F with regards to highlighting portions of display that have
 35027    mouse-face properties.  Also de-highlight portions of display where
 35028    the mouse was before, set the mouse pointer shape as appropriate
 35029    for the mouse coordinates, and activate help echo (tooltips).
 35030    X and Y can be negative or out of range.  */
 35031 
 35032 void
 35033 note_mouse_highlight (struct frame *f, int x, int y)
 35034 {
 35035   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35036   enum window_part part = ON_NOTHING;
 35037   Lisp_Object window;
 35038   struct window *w;
 35039   Emacs_Cursor cursor = No_Cursor;
 35040   Lisp_Object pointer = Qnil;  /* Takes precedence over cursor!  */
 35041   struct buffer *b;
 35042 
 35043   /* When a menu is active, don't highlight because this looks odd.  */
 35044 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NS) || defined (MSDOS)
 35045   if (popup_activated ())
 35046     return;
 35047 #endif
 35048 
 35049 #if defined (HAVE_HAIKU)
 35050   if (popup_activated_p)
 35051     return;
 35052 #endif
 35053 
 35054   if (!f->glyphs_initialized_p
 35055       || f->pointer_invisible)
 35056     return;
 35057 
 35058   hlinfo->mouse_face_mouse_x = x;
 35059   hlinfo->mouse_face_mouse_y = y;
 35060   hlinfo->mouse_face_mouse_frame = f;
 35061 
 35062   if (hlinfo->mouse_face_defer)
 35063     return;
 35064 
 35065   /* Which window is that in?  */
 35066   window = window_from_coordinates (f, x, y, &part, true, true);
 35067 
 35068   /* If displaying active text in another window, clear that.  */
 35069   if (! EQ (window, hlinfo->mouse_face_window)
 35070       /* Also clear if we move out of text area in same window.  */
 35071       || (!NILP (hlinfo->mouse_face_window)
 35072           && !NILP (window)
 35073           && part != ON_TEXT
 35074           && part != ON_MODE_LINE
 35075           && part != ON_HEADER_LINE
 35076           && part != ON_TAB_LINE))
 35077     clear_mouse_face (hlinfo);
 35078 
 35079   /* Reset help_echo_string.  It will get recomputed below.  */
 35080   help_echo_string = Qnil;
 35081 
 35082   /* Handle tab-bar highlight on mouse-capable TTY frames.  */
 35083   if (!FRAME_WINDOW_P (f)
 35084       && (y >= FRAME_MENU_BAR_LINES (f)
 35085           && y < FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f)))
 35086     {
 35087       int prop_idx;
 35088       bool ignore;
 35089       Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &ignore);
 35090 
 35091       if (!NILP (caption))
 35092         {
 35093           help_echo_object = help_echo_window = Qnil;
 35094           help_echo_pos = -1;
 35095           help_echo_string = AREF (f->tab_bar_items,
 35096                                    prop_idx * TAB_BAR_ITEM_NSLOTS
 35097                                    + TAB_BAR_ITEM_HELP);
 35098           if (NILP (help_echo_string))
 35099             help_echo_string = caption;
 35100         }
 35101     }
 35102 
 35103 #ifdef HAVE_WINDOW_SYSTEM
 35104   /* If the cursor is on the internal border of FRAME and FRAME's
 35105      internal border is draggable, provide some visual feedback.  */
 35106   if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
 35107       && !NILP (get_frame_param (f, Qdrag_internal_border)))
 35108     {
 35109       enum internal_border_part part = frame_internal_border_part (f, x, y);
 35110 
 35111       switch (part)
 35112         {
 35113         case INTERNAL_BORDER_NONE:
 35114           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35115             /* Reset cursor.  */
 35116             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35117           break;
 35118         case INTERNAL_BORDER_LEFT_EDGE:
 35119           cursor = FRAME_OUTPUT_DATA (f)->left_edge_cursor;
 35120           break;
 35121         case INTERNAL_BORDER_TOP_LEFT_CORNER:
 35122           cursor = FRAME_OUTPUT_DATA (f)->top_left_corner_cursor;
 35123           break;
 35124         case INTERNAL_BORDER_TOP_EDGE:
 35125           cursor = FRAME_OUTPUT_DATA (f)->top_edge_cursor;
 35126           break;
 35127         case INTERNAL_BORDER_TOP_RIGHT_CORNER:
 35128           cursor = FRAME_OUTPUT_DATA (f)->top_right_corner_cursor;
 35129           break;
 35130         case INTERNAL_BORDER_RIGHT_EDGE:
 35131           cursor = FRAME_OUTPUT_DATA (f)->right_edge_cursor;
 35132           break;
 35133         case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
 35134           cursor = FRAME_OUTPUT_DATA (f)->bottom_right_corner_cursor;
 35135           break;
 35136         case INTERNAL_BORDER_BOTTOM_EDGE:
 35137           cursor = FRAME_OUTPUT_DATA (f)->bottom_edge_cursor;
 35138           break;
 35139         case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
 35140           cursor = FRAME_OUTPUT_DATA (f)->bottom_left_corner_cursor;
 35141           break;
 35142         default:
 35143           /* This should not happen.  */
 35144           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35145             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35146         }
 35147 
 35148       if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35149         {
 35150           /* Do we really want a help echo here?  */
 35151           help_echo_string = build_string ("drag-mouse-1: resize frame");
 35152           goto set_cursor;
 35153         }
 35154     }
 35155 #endif /* HAVE_WINDOW_SYSTEM */
 35156 
 35157   /* Not on a window -> return.  */
 35158   if (!WINDOWP (window))
 35159     return;
 35160 
 35161   /* Convert to window-relative pixel coordinates.  */
 35162   w = XWINDOW (window);
 35163   frame_to_window_pixel_xy (w, &x, &y);
 35164 
 35165 #if defined (HAVE_WINDOW_SYSTEM)
 35166   /* Handle tab-bar window differently since it doesn't display a
 35167      buffer.  */
 35168   if (EQ (window, f->tab_bar_window))
 35169     {
 35170       note_tab_bar_highlight (f, x, y);
 35171       if (tab_bar__dragging_in_progress)
 35172         {
 35173           cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35174           goto set_cursor;
 35175         }
 35176       else
 35177         return;
 35178     }
 35179   else
 35180     {
 35181       /* The mouse might have pressed into the tab bar, but might
 35182          also have been released outside the tab bar, so
 35183          f->last_tab_bar_item must be reset, in order to make sure the
 35184          item can be still highlighted again in the future.  */
 35185       f->last_tab_bar_item = -1;
 35186     }
 35187 #endif
 35188 
 35189 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 35190   /* Handle tool-bar window differently since it doesn't display a
 35191      buffer.  */
 35192   if (EQ (window, f->tool_bar_window))
 35193     {
 35194       note_tool_bar_highlight (f, x, y);
 35195       return;
 35196     }
 35197 #endif
 35198 
 35199   /* Mouse is on the mode, header line or margin?  */
 35200   if (part == ON_MODE_LINE || part == ON_HEADER_LINE || part == ON_TAB_LINE
 35201       || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35202     {
 35203       note_mode_line_or_margin_highlight (window, x, y, part);
 35204 
 35205 #ifdef HAVE_WINDOW_SYSTEM
 35206       if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35207         {
 35208           cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35209           /* Show non-text cursor (Bug#16647).  */
 35210           goto set_cursor;
 35211         }
 35212       else
 35213 #endif
 35214         return;
 35215     }
 35216 
 35217 #ifdef HAVE_WINDOW_SYSTEM
 35218   if (part == ON_VERTICAL_BORDER)
 35219     {
 35220       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35221       help_echo_string = build_string ("drag-mouse-1: resize");
 35222       goto set_cursor;
 35223     }
 35224   else if (part == ON_RIGHT_DIVIDER)
 35225     {
 35226       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35227       help_echo_string = build_string ("drag-mouse-1: resize");
 35228       goto set_cursor;
 35229     }
 35230   else if (part == ON_BOTTOM_DIVIDER)
 35231     if (! WINDOW_BOTTOMMOST_P (w)
 35232         || minibuf_level
 35233         || NILP (Vresize_mini_windows))
 35234       {
 35235         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35236         help_echo_string = build_string ("drag-mouse-1: resize");
 35237         goto set_cursor;
 35238       }
 35239     else
 35240       cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35241   else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
 35242            || part == ON_VERTICAL_SCROLL_BAR
 35243            || part == ON_HORIZONTAL_SCROLL_BAR)
 35244     cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35245   else
 35246     cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35247 #endif
 35248 
 35249   /* Are we in a window whose display is up to date?
 35250      And verify the buffer's text has not changed.  */
 35251   b = XBUFFER (w->contents);
 35252   if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
 35253     {
 35254       int hpos, vpos, dx, dy, area = LAST_AREA;
 35255       ptrdiff_t pos;
 35256       struct glyph *glyph;
 35257       Lisp_Object object;
 35258       Lisp_Object mouse_face = Qnil, position;
 35259       Lisp_Object *overlay_vec = NULL;
 35260       ptrdiff_t i, noverlays;
 35261       struct buffer *obuf;
 35262       ptrdiff_t obegv, ozv;
 35263       bool same_region;
 35264 
 35265       /* Find the glyph under X/Y.  */
 35266       glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
 35267 
 35268 #ifdef HAVE_WINDOW_SYSTEM
 35269       /* Look for :pointer property on image.  */
 35270       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 35271         {
 35272           struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 35273           if (img != NULL && IMAGEP (img->spec))
 35274             {
 35275               Lisp_Object image_map, hotspot;
 35276               if ((image_map = plist_get (XCDR (img->spec), QCmap),
 35277                    !NILP (image_map))
 35278                   && (hotspot = find_hot_spot (image_map,
 35279                                                glyph->slice.img.x + dx,
 35280                                                glyph->slice.img.y + dy),
 35281                       CONSP (hotspot))
 35282                   && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35283                 {
 35284                   Lisp_Object plist;
 35285 
 35286                   /* Could check XCAR (hotspot) to see if we enter/leave
 35287                      this hot-spot.
 35288                      If so, we could look for mouse-enter, mouse-leave
 35289                      properties in PLIST (and do something...).  */
 35290                   hotspot = XCDR (hotspot);
 35291                   if (CONSP (hotspot)
 35292                       && (plist = XCAR (hotspot), CONSP (plist)))
 35293                     {
 35294                       pointer = plist_get (plist, Qpointer);
 35295                       if (NILP (pointer))
 35296                         pointer = Qhand;
 35297                       help_echo_string = plist_get (plist, Qhelp_echo);
 35298                       if (!NILP (help_echo_string))
 35299                         {
 35300                           help_echo_window = window;
 35301                           help_echo_object = glyph->object;
 35302                           help_echo_pos = glyph->charpos;
 35303                         }
 35304                     }
 35305                 }
 35306               if (NILP (pointer))
 35307                 pointer = plist_get (XCDR (img->spec), QCpointer);
 35308             }
 35309         }
 35310 #endif  /* HAVE_WINDOW_SYSTEM */
 35311 
 35312       /* Clear mouse face if X/Y not over text.  */
 35313       if (glyph == NULL
 35314           || area != TEXT_AREA
 35315           || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
 35316           /* Glyph's OBJECT is nil for glyphs inserted by the
 35317              display engine for its internal purposes, like truncation
 35318              and continuation glyphs and blanks beyond the end of
 35319              line's text on text terminals.  If we are over such a
 35320              glyph, we are not over any text.  */
 35321           || NILP (glyph->object)
 35322           /* R2L rows have a stretch glyph at their front, which
 35323              stands for no text, whereas L2R rows have no glyphs at
 35324              all beyond the end of text.  Treat such stretch glyphs
 35325              like we do with NULL glyphs in L2R rows.  */
 35326           || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
 35327               && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
 35328               && glyph->type == STRETCH_GLYPH
 35329               && glyph->avoid_cursor_p))
 35330         {
 35331           if (clear_mouse_face (hlinfo))
 35332             cursor = No_Cursor;
 35333           if (FRAME_WINDOW_P (f) && NILP (pointer))
 35334             {
 35335 #ifdef HAVE_WINDOW_SYSTEM
 35336               if (area != TEXT_AREA)
 35337                 cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35338               else
 35339                 pointer = Vvoid_text_area_pointer;
 35340 #endif
 35341             }
 35342           goto set_cursor;
 35343         }
 35344 
 35345       pos = glyph->charpos;
 35346       object = glyph->object;
 35347       if (!STRINGP (object) && !BUFFERP (object))
 35348         goto set_cursor;
 35349 
 35350       /* If we get an out-of-range value, return now; avoid an error.  */
 35351       if (BUFFERP (object) && pos > BUF_Z (b))
 35352         goto set_cursor;
 35353 
 35354       /* Make the window's buffer temporarily current for
 35355          overlays_at and compute_char_face.  */
 35356       obuf = current_buffer;
 35357       current_buffer = b;
 35358       obegv = BEGV;
 35359       ozv = ZV;
 35360       BEGV = BEG;
 35361       ZV = Z;
 35362 
 35363       /* Is this char mouse-active or does it have help-echo?  */
 35364       position = make_fixnum (pos);
 35365 
 35366       USE_SAFE_ALLOCA;
 35367 
 35368       if (BUFFERP (object))
 35369         {
 35370           /* Put all the overlays we want in a vector in overlay_vec.  */
 35371           GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL);
 35372           /* Sort overlays into increasing priority order.  */
 35373           noverlays = sort_overlays (overlay_vec, noverlays, w);
 35374         }
 35375       else
 35376         noverlays = 0;
 35377 
 35378       if (NILP (Vmouse_highlight))
 35379         {
 35380           clear_mouse_face (hlinfo);
 35381           goto check_help_echo;
 35382         }
 35383 
 35384       same_region = coords_in_mouse_face_p (w, hpos, vpos);
 35385 
 35386       if (same_region)
 35387         cursor = No_Cursor;
 35388 
 35389       /* Check mouse-face highlighting.  */
 35390       if (! same_region
 35391           /* If there exists an overlay with mouse-face overlapping
 35392              the one we are currently highlighting, we have to check
 35393              if we enter the overlapping overlay, and then highlight
 35394              only that.  Skip the check when mouse-face highlighting
 35395              is currently hidden to avoid Bug#30519.  */
 35396           || (!hlinfo->mouse_face_hidden
 35397               && OVERLAYP (hlinfo->mouse_face_overlay)
 35398               /* It's possible the overlay was deleted (Bug#35273).  */
 35399               && OVERLAY_BUFFER (hlinfo->mouse_face_overlay)
 35400               && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
 35401         {
 35402           /* Find the highest priority overlay with a mouse-face.  */
 35403           Lisp_Object overlay = Qnil;
 35404           for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
 35405             {
 35406               mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
 35407               if (!NILP (mouse_face))
 35408                 overlay = overlay_vec[i];
 35409             }
 35410 
 35411           /* If we're highlighting the same overlay as before, there's
 35412              no need to do that again.  */
 35413           if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
 35414             goto check_help_echo;
 35415 
 35416           /* Clear the display of the old active region, if any.  */
 35417           if (clear_mouse_face (hlinfo))
 35418             cursor = No_Cursor;
 35419 
 35420           /* Record the overlay, if any, to be highlighted.  */
 35421           hlinfo->mouse_face_overlay = overlay;
 35422 
 35423           /* If no overlay applies, get a text property.  */
 35424           if (NILP (overlay))
 35425             mouse_face = Fget_text_property (position, Qmouse_face, object);
 35426 
 35427           /* Next, compute the bounds of the mouse highlighting and
 35428              display it.  */
 35429           if (!NILP (mouse_face) && STRINGP (object))
 35430             {
 35431               /* The mouse-highlighting comes from a display string
 35432                  with a mouse-face.  */
 35433               Lisp_Object s, e;
 35434               ptrdiff_t ignore;
 35435 
 35436               s = Fprevious_single_property_change
 35437                 (make_fixnum (pos + 1), Qmouse_face, object, Qnil);
 35438               e = Fnext_single_property_change
 35439                 (position, Qmouse_face, object, Qnil);
 35440               if (NILP (s))
 35441                 s = make_fixnum (0);
 35442               if (NILP (e))
 35443                 e = make_fixnum (SCHARS (object));
 35444               mouse_face_from_string_pos (w, hlinfo, object,
 35445                                           XFIXNUM (s), XFIXNUM (e));
 35446               hlinfo->mouse_face_past_end = false;
 35447               hlinfo->mouse_face_window = window;
 35448               hlinfo->mouse_face_face_id
 35449                 = face_at_string_position (w, object, pos, 0, &ignore,
 35450                                            glyph->face_id, true, 0);
 35451               show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35452               cursor = No_Cursor;
 35453             }
 35454           else
 35455             {
 35456               /* The mouse-highlighting, if any, comes from an overlay
 35457                  or text property in the buffer.  */
 35458               Lisp_Object buffer UNINIT;
 35459               Lisp_Object disp_string UNINIT;
 35460 
 35461               if (STRINGP (object))
 35462                 {
 35463                   /* If we are on a display string with no mouse-face,
 35464                      check if the text under it has one.  */
 35465                   struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
 35466                   ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35467                   pos = string_buffer_position (object, start);
 35468                   if (pos > 0)
 35469                     {
 35470                       mouse_face = get_char_property_and_overlay
 35471                         (make_fixnum (pos), Qmouse_face, w->contents, &overlay);
 35472                       buffer = w->contents;
 35473                       disp_string = object;
 35474                     }
 35475                 }
 35476               else
 35477                 {
 35478                   buffer = object;
 35479                   disp_string = Qnil;
 35480                 }
 35481 
 35482               if (!NILP (mouse_face))
 35483                 {
 35484                   Lisp_Object before, after;
 35485                   Lisp_Object before_string, after_string;
 35486                   /* To correctly find the limits of mouse highlight
 35487                      in a bidi-reordered buffer, we must not use the
 35488                      optimization of limiting the search in
 35489                      previous-single-property-change and
 35490                      next-single-property-change, because
 35491                      rows_from_pos_range needs the real start and end
 35492                      positions to DTRT in this case.  That's because
 35493                      the first row visible in a window does not
 35494                      necessarily display the character whose position
 35495                      is the smallest.  */
 35496                   Lisp_Object lim1
 35497                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35498                     ? Fmarker_position (w->start)
 35499                     : Qnil;
 35500                   Lisp_Object lim2
 35501                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35502                     ? make_fixnum (BUF_Z (XBUFFER (buffer))
 35503                                    - w->window_end_pos)
 35504                     : Qnil;
 35505 
 35506                   if (NILP (overlay))
 35507                     {
 35508                       /* Handle the text property case.  */
 35509                       before = Fprevious_single_property_change
 35510                         (make_fixnum (pos + 1), Qmouse_face, buffer, lim1);
 35511                       after = Fnext_single_property_change
 35512                         (make_fixnum (pos), Qmouse_face, buffer, lim2);
 35513                       before_string = after_string = Qnil;
 35514                     }
 35515                   else
 35516                     {
 35517                       /* Handle the overlay case.  */
 35518                       before = Foverlay_start (overlay);
 35519                       after = Foverlay_end (overlay);
 35520                       before_string = Foverlay_get (overlay, Qbefore_string);
 35521                       after_string = Foverlay_get (overlay, Qafter_string);
 35522 
 35523                       if (!STRINGP (before_string)) before_string = Qnil;
 35524                       if (!STRINGP (after_string))  after_string = Qnil;
 35525                     }
 35526 
 35527                   mouse_face_from_buffer_pos (window, hlinfo, pos,
 35528                                               NILP (before)
 35529                                               ? 1
 35530                                               : XFIXNAT (before),
 35531                                               NILP (after)
 35532                                               ? BUF_Z (XBUFFER (buffer))
 35533                                               : XFIXNAT (after),
 35534                                               before_string, after_string,
 35535                                               disp_string);
 35536                   cursor = No_Cursor;
 35537                 }
 35538             }
 35539         }
 35540 
 35541     check_help_echo:
 35542 
 35543       /* Look for a `help-echo' property.  */
 35544       if (NILP (help_echo_string)) {
 35545         Lisp_Object help, overlay;
 35546 
 35547         /* Check overlays first.  */
 35548         help = overlay = Qnil;
 35549         for (i = noverlays - 1; i >= 0 && NILP (help); --i)
 35550           {
 35551             overlay = overlay_vec[i];
 35552             help = Foverlay_get (overlay, Qhelp_echo);
 35553           }
 35554 
 35555         if (!NILP (help))
 35556           {
 35557             help_echo_string = help;
 35558             help_echo_window = window;
 35559             help_echo_object = overlay;
 35560             help_echo_pos = pos;
 35561           }
 35562         else
 35563           {
 35564             Lisp_Object obj = glyph->object;
 35565             ptrdiff_t charpos = glyph->charpos;
 35566 
 35567             /* Try text properties.  */
 35568             if (STRINGP (obj)
 35569                 && charpos >= 0
 35570                 && charpos < SCHARS (obj))
 35571               {
 35572                 help = Fget_text_property (make_fixnum (charpos),
 35573                                            Qhelp_echo, obj);
 35574                 if (NILP (help))
 35575                   {
 35576                     /* If the string itself doesn't specify a help-echo,
 35577                        see if the buffer text ``under'' it does.  */
 35578                     struct glyph_row *r
 35579                       = MATRIX_ROW (w->current_matrix, vpos);
 35580                     ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35581                     ptrdiff_t p = string_buffer_position (obj, start);
 35582                     if (p > 0)
 35583                       {
 35584                         help = Fget_char_property (make_fixnum (p),
 35585                                                    Qhelp_echo, w->contents);
 35586                         if (!NILP (help))
 35587                           {
 35588                             charpos = p;
 35589                             obj = w->contents;
 35590                           }
 35591                       }
 35592                   }
 35593               }
 35594             else if (BUFFERP (obj)
 35595                      && charpos >= BEGV
 35596                      && charpos < ZV)
 35597               help = Fget_text_property (make_fixnum (charpos), Qhelp_echo,
 35598                                          obj);
 35599 
 35600             if (!NILP (help))
 35601               {
 35602                 help_echo_string = help;
 35603                 help_echo_window = window;
 35604                 help_echo_object = obj;
 35605                 help_echo_pos = charpos;
 35606               }
 35607           }
 35608       }
 35609 
 35610 #ifdef HAVE_WINDOW_SYSTEM
 35611       /* Look for a `pointer' property.  */
 35612       if (FRAME_WINDOW_P (f) && NILP (pointer))
 35613         {
 35614           /* Check overlays first.  */
 35615           for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
 35616             pointer = Foverlay_get (overlay_vec[i], Qpointer);
 35617 
 35618           if (NILP (pointer))
 35619             {
 35620               Lisp_Object obj = glyph->object;
 35621               ptrdiff_t charpos = glyph->charpos;
 35622 
 35623               /* Try text properties.  */
 35624               if (STRINGP (obj)
 35625                   && charpos >= 0
 35626                   && charpos < SCHARS (obj))
 35627                 {
 35628                   pointer = Fget_text_property (make_fixnum (charpos),
 35629                                                 Qpointer, obj);
 35630                   if (NILP (pointer))
 35631                     {
 35632                       /* If the string itself doesn't specify a pointer,
 35633                          see if the buffer text ``under'' it does.  */
 35634                       struct glyph_row *r
 35635                         = MATRIX_ROW (w->current_matrix, vpos);
 35636                       ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35637                       ptrdiff_t p = string_buffer_position (obj, start);
 35638                       if (p > 0)
 35639                         pointer = Fget_char_property (make_fixnum (p),
 35640                                                       Qpointer, w->contents);
 35641                     }
 35642                 }
 35643               else if (BUFFERP (obj)
 35644                        && charpos >= BEGV
 35645                        && charpos < ZV)
 35646                 pointer = Fget_text_property (make_fixnum (charpos),
 35647                                               Qpointer, obj);
 35648             }
 35649         }
 35650 #endif  /* HAVE_WINDOW_SYSTEM */
 35651 
 35652       BEGV = obegv;
 35653       ZV = ozv;
 35654       current_buffer = obuf;
 35655       SAFE_FREE ();
 35656     }
 35657 
 35658  set_cursor:
 35659   define_frame_cursor1 (f, cursor, pointer);
 35660 }
 35661 
 35662 
 35663 /* EXPORT for RIF:
 35664    Clear any mouse-face on window W.  This function is part of the
 35665    redisplay interface, and is called from try_window_id and similar
 35666    functions to ensure the mouse-highlight is off.  */
 35667 
 35668 void
 35669 gui_clear_window_mouse_face (struct window *w)
 35670 {
 35671   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 35672   Lisp_Object window;
 35673 
 35674   block_input ();
 35675   XSETWINDOW (window, w);
 35676   if (EQ (window, hlinfo->mouse_face_window))
 35677     clear_mouse_face (hlinfo);
 35678   unblock_input ();
 35679 }
 35680 
 35681 
 35682 /* EXPORT:
 35683    Just discard the mouse face information for frame F, if any.
 35684    This is used when the size of F is changed.  */
 35685 
 35686 void
 35687 cancel_mouse_face (struct frame *f)
 35688 {
 35689   Lisp_Object window;
 35690   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35691 
 35692   window = hlinfo->mouse_face_window;
 35693   if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
 35694     reset_mouse_highlight (hlinfo);
 35695 }
 35696 
 35697 
 35698 
 35699 /***********************************************************************
 35700                            Exposure Events
 35701  ***********************************************************************/
 35702 
 35703 #ifdef HAVE_WINDOW_SYSTEM
 35704 
 35705 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
 35706    which intersects rectangle R.  R is in window-relative coordinates.  */
 35707 
 35708 static void
 35709 expose_area (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r,
 35710              enum glyph_row_area area)
 35711 {
 35712   struct glyph *first = row->glyphs[area];
 35713   struct glyph *end = row->glyphs[area] + row->used[area];
 35714   struct glyph *last;
 35715   int first_x, start_x, x;
 35716 
 35717   if (area == TEXT_AREA && row->fill_line_p)
 35718     /* If row extends face to end of line write the whole line.  */
 35719     draw_glyphs (w, row->x, row, area,
 35720                  0, row->used[area],
 35721                  DRAW_NORMAL_TEXT, 0);
 35722   else
 35723     {
 35724       /* Set START_X to the window-relative start position for drawing glyphs of
 35725          AREA.  The first glyph of the text area can be partially visible.
 35726          The first glyphs of other areas cannot.  */
 35727       start_x = window_box_left_offset (w, area);
 35728       x = start_x;
 35729       if (area == TEXT_AREA)
 35730         x += row->x;
 35731 
 35732       /* Find the first glyph that must be redrawn.  */
 35733       while (first < end
 35734              && x + first->pixel_width < r->x)
 35735         {
 35736           x += first->pixel_width;
 35737           ++first;
 35738         }
 35739 
 35740       /* Find the last one.  */
 35741       last = first;
 35742       first_x = x;
 35743       /* Use a signed int intermediate value to avoid catastrophic
 35744          failures due to comparison between signed and unsigned, when
 35745          x is negative (can happen for wide images that are hscrolled).  */
 35746       int r_end = r->x + r->width;
 35747       while (last < end && x < r_end)
 35748         {
 35749           x += last->pixel_width;
 35750           ++last;
 35751         }
 35752 
 35753       /* Repaint.  */
 35754       if (last > first)
 35755         draw_glyphs (w, first_x - start_x, row, area,
 35756                      first - row->glyphs[area], last - row->glyphs[area],
 35757                      DRAW_NORMAL_TEXT, 0);
 35758     }
 35759 }
 35760 
 35761 
 35762 /* Redraw the parts of the glyph row ROW on window W intersecting
 35763    rectangle R.  R is in window-relative coordinates.  Value is
 35764    true if mouse-face was overwritten.  */
 35765 
 35766 static bool
 35767 expose_line (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r)
 35768 {
 35769   eassert (row->enabled_p);
 35770 
 35771   if (row->mode_line_p || w->pseudo_window_p)
 35772     draw_glyphs (w, 0, row, TEXT_AREA,
 35773                  0, row->used[TEXT_AREA],
 35774                  DRAW_NORMAL_TEXT, 0);
 35775   else
 35776     {
 35777       if (row->used[LEFT_MARGIN_AREA])
 35778         expose_area (w, row, r, LEFT_MARGIN_AREA);
 35779       if (row->used[TEXT_AREA])
 35780         expose_area (w, row, r, TEXT_AREA);
 35781       if (row->used[RIGHT_MARGIN_AREA])
 35782         expose_area (w, row, r, RIGHT_MARGIN_AREA);
 35783       draw_row_fringe_bitmaps (w, row);
 35784     }
 35785 
 35786   return row->mouse_face_p;
 35787 }
 35788 
 35789 
 35790 /* Redraw those parts of glyphs rows during expose event handling that
 35791    overlap other rows.  Redrawing of an exposed line writes over parts
 35792    of lines overlapping that exposed line; this function fixes that.
 35793 
 35794    W is the window being exposed.  FIRST_OVERLAPPING_ROW is the first
 35795    row in W's current matrix that is exposed and overlaps other rows.
 35796    LAST_OVERLAPPING_ROW is the last such row.  */
 35797 
 35798 static void
 35799 expose_overlaps (struct window *w,
 35800                  struct glyph_row *first_overlapping_row,
 35801                  struct glyph_row *last_overlapping_row,
 35802                  const Emacs_Rectangle *r)
 35803 {
 35804   struct glyph_row *row;
 35805 
 35806   for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
 35807     if (row->overlapping_p)
 35808       {
 35809         eassert (row->enabled_p && !row->mode_line_p);
 35810 
 35811         row->clip = r;
 35812         if (row->used[LEFT_MARGIN_AREA])
 35813           gui_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
 35814 
 35815         if (row->used[TEXT_AREA])
 35816           gui_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
 35817 
 35818         if (row->used[RIGHT_MARGIN_AREA])
 35819           gui_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
 35820         row->clip = NULL;
 35821       }
 35822 }
 35823 
 35824 
 35825 /* Return true if W's cursor intersects rectangle R.  */
 35826 
 35827 static bool
 35828 phys_cursor_in_rect_p (struct window *w, const Emacs_Rectangle *r)
 35829 {
 35830   Emacs_Rectangle cr, result;
 35831   struct glyph *cursor_glyph;
 35832   struct glyph_row *row;
 35833 
 35834   if (w->phys_cursor.vpos >= 0
 35835       && w->phys_cursor.vpos < w->current_matrix->nrows
 35836       && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
 35837           row->enabled_p)
 35838       && row->cursor_in_fringe_p)
 35839     {
 35840       /* Cursor is in the fringe.  */
 35841       cr.x = window_box_right_offset (w,
 35842                                       (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
 35843                                        ? RIGHT_MARGIN_AREA
 35844                                        : TEXT_AREA));
 35845       cr.y = row->y;
 35846       cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
 35847       cr.height = row->height;
 35848       return gui_intersect_rectangles (&cr, r, &result);
 35849     }
 35850 
 35851   cursor_glyph = get_phys_cursor_glyph (w);
 35852   if (cursor_glyph)
 35853     {
 35854       /* r is relative to W's box, but w->phys_cursor.x is relative
 35855          to left edge of W's TEXT area.  Adjust it.  */
 35856       cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
 35857       cr.y = w->phys_cursor.y;
 35858       cr.width = cursor_glyph->pixel_width;
 35859       cr.height = w->phys_cursor_height;
 35860       /* ++KFS: W32 version used W32-specific IntersectRect here, but
 35861          I assume the effect is the same -- and this is portable.  */
 35862       return gui_intersect_rectangles (&cr, r, &result);
 35863     }
 35864   /* If we don't understand the format, pretend we're not in the hot-spot.  */
 35865   return false;
 35866 }
 35867 
 35868 
 35869 /* EXPORT:
 35870    Draw a vertical window border to the right of window W if W doesn't
 35871    have vertical scroll bars.  */
 35872 
 35873 void
 35874 gui_draw_vertical_border (struct window *w)
 35875 {
 35876   struct frame *f = XFRAME (WINDOW_FRAME (w));
 35877 
 35878   /* We could do better, if we knew what type of scroll-bar the adjacent
 35879      windows (on either side) have...  But we don't :-(
 35880      However, I think this works ok.  ++KFS 2003-04-25 */
 35881 
 35882   /* Redraw borders between horizontally adjacent windows.  Don't
 35883      do it for frames with vertical scroll bars because either the
 35884      right scroll bar of a window, or the left scroll bar of its
 35885      neighbor will suffice as a border.  */
 35886   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
 35887     return;
 35888 
 35889   /* Note: It is necessary to redraw both the left and the right
 35890      borders, for when only this single window W is being
 35891      redisplayed.  */
 35892   if (!WINDOW_RIGHTMOST_P (w)
 35893       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
 35894     {
 35895       int x0, x1, y0, y1;
 35896 
 35897       window_box_edges (w, &x0, &y0, &x1, &y1);
 35898       y1 -= 1;
 35899 
 35900       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 35901         x1 -= 1;
 35902 
 35903       FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
 35904     }
 35905 
 35906   if (!WINDOW_LEFTMOST_P (w)
 35907       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
 35908     {
 35909       int x0, x1, y0, y1;
 35910 
 35911       window_box_edges (w, &x0, &y0, &x1, &y1);
 35912       y1 -= 1;
 35913 
 35914       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 35915         x0 -= 1;
 35916 
 35917       FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
 35918     }
 35919 }
 35920 
 35921 
 35922 /* Draw window dividers for window W.  */
 35923 
 35924 void
 35925 gui_draw_right_divider (struct window *w)
 35926 {
 35927   struct frame *f = WINDOW_XFRAME (w);
 35928 
 35929   if (w->mini || w->pseudo_window_p)
 35930     return;
 35931   else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 35932     {
 35933       int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
 35934       int x1 = WINDOW_RIGHT_EDGE_X (w);
 35935       int y0 = WINDOW_TOP_EDGE_Y (w);
 35936       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 35937 
 35938       /* If W is horizontally combined and has a right sibling, don't
 35939          draw over any bottom divider.  */
 35940       if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
 35941           && !NILP (w->parent)
 35942           && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
 35943           && !NILP (w->next))
 35944         y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 35945 
 35946       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 35947     }
 35948 }
 35949 
 35950 static void
 35951 gui_draw_bottom_divider (struct window *w)
 35952 {
 35953   struct frame *f = XFRAME (WINDOW_FRAME (w));
 35954 
 35955   if (w->mini || w->pseudo_window_p)
 35956     return;
 35957   else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 35958     {
 35959       int x0 = WINDOW_LEFT_EDGE_X (w);
 35960       int x1 = WINDOW_RIGHT_EDGE_X (w);
 35961       int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 35962       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 35963       struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
 35964 
 35965       /* If W is vertically combined and has a sibling below, don't draw
 35966          over any right divider.  */
 35967       if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
 35968           && p
 35969           && ((WINDOW_VERTICAL_COMBINATION_P (p)
 35970                && !NILP (w->next))
 35971               || (WINDOW_HORIZONTAL_COMBINATION_P (p)
 35972                   && NILP (w->next)
 35973                   && !NILP (p->parent)
 35974                   && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
 35975                   && !NILP (XWINDOW (p->parent)->next))))
 35976         x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
 35977 
 35978       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 35979     }
 35980 }
 35981 
 35982 /* Redraw the part of window W intersection rectangle FR.  Pixel
 35983    coordinates in FR are frame-relative.  Call this function with
 35984    input blocked.  Value is true if the exposure overwrites
 35985    mouse-face.  */
 35986 
 35987 static bool
 35988 expose_window (struct window *w, const Emacs_Rectangle *fr)
 35989 {
 35990   struct frame *f = XFRAME (w->frame);
 35991   Emacs_Rectangle wr, r;
 35992   bool mouse_face_overwritten_p = false;
 35993 
 35994   /* If window is not yet fully initialized, do nothing.  This can
 35995      happen when toolkit scroll bars are used and a window is split.
 35996      Reconfiguring the scroll bar will generate an expose for a newly
 35997      created window.  */
 35998   if (w->current_matrix == NULL)
 35999     return false;
 36000 
 36001   /* When we're currently updating the window, display and current
 36002      matrix usually don't agree.  Arrange for a thorough display
 36003      later.  */
 36004   if (w->must_be_updated_p)
 36005     {
 36006       SET_FRAME_GARBAGED (f);
 36007       return false;
 36008     }
 36009 
 36010   /* Frame-relative pixel rectangle of W.  */
 36011   wr.x = WINDOW_LEFT_EDGE_X (w);
 36012   wr.y = WINDOW_TOP_EDGE_Y (w);
 36013   wr.width = WINDOW_PIXEL_WIDTH (w);
 36014   wr.height = WINDOW_PIXEL_HEIGHT (w);
 36015 
 36016   if (gui_intersect_rectangles (fr, &wr, &r))
 36017     {
 36018       int yb = window_text_bottom_y (w);
 36019       struct glyph_row *row;
 36020       struct glyph_row *first_overlapping_row, *last_overlapping_row;
 36021 
 36022       redisplay_trace ("expose_window (%d, %d, %u, %u)\n",
 36023                        r.x, r.y, r.width, r.height);
 36024 
 36025       /* Convert to window coordinates.  */
 36026       r.x -= WINDOW_LEFT_EDGE_X (w);
 36027       r.y -= WINDOW_TOP_EDGE_Y (w);
 36028 
 36029       /* Turn off the cursor.  */
 36030       bool cursor_cleared_p = (!w->pseudo_window_p
 36031                                && phys_cursor_in_rect_p (w, &r));
 36032       if (cursor_cleared_p)
 36033         gui_clear_cursor (w);
 36034 
 36035       /* If the row containing the cursor extends face to end of line,
 36036          then expose_area might overwrite the cursor outside the
 36037          rectangle and thus notice_overwritten_cursor might clear
 36038          w->phys_cursor_on_p.  We remember the original value and
 36039          check later if it is changed.  */
 36040       bool phys_cursor_on_p = w->phys_cursor_on_p;
 36041 
 36042       /* Use a signed int intermediate value to avoid catastrophic
 36043          failures due to comparison between signed and unsigned, when
 36044          y0 or y1 is negative (can happen for tall images).  */
 36045       int r_bottom = r.y + r.height;
 36046 
 36047       /* We must temporarily switch to the window's buffer, in case
 36048          the fringe face has been remapped in that buffer's
 36049          face-remapping-alist, so that draw_row_fringe_bitmaps,
 36050          called from expose_line, will use the right face.  */
 36051       bool buffer_changed = false;
 36052       struct buffer *oldbuf = current_buffer;
 36053       if (!w->pseudo_window_p)
 36054         {
 36055           set_buffer_internal_1 (XBUFFER (w->contents));
 36056           buffer_changed = true;
 36057         }
 36058 
 36059       /* Update lines intersecting rectangle R.  */
 36060       first_overlapping_row = last_overlapping_row = NULL;
 36061       for (row = w->current_matrix->rows;
 36062            row->enabled_p;
 36063            ++row)
 36064         {
 36065           int y0 = row->y;
 36066           int y1 = MATRIX_ROW_BOTTOM_Y (row);
 36067 
 36068           if ((y0 >= r.y && y0 < r_bottom)
 36069               || (y1 > r.y && y1 < r_bottom)
 36070               || (r.y >= y0 && r.y < y1)
 36071               || (r_bottom > y0 && r_bottom < y1))
 36072             {
 36073               /* A header line may be overlapping, but there is no need
 36074                  to fix overlapping areas for them.  KFS 2005-02-12 */
 36075               if (row->overlapping_p && !row->mode_line_p)
 36076                 {
 36077                   if (first_overlapping_row == NULL)
 36078                     first_overlapping_row = row;
 36079                   last_overlapping_row = row;
 36080                 }
 36081 
 36082               row->clip = fr;
 36083               if (expose_line (w, row, &r))
 36084                 mouse_face_overwritten_p = true;
 36085               row->clip = NULL;
 36086             }
 36087           else if (row->overlapping_p)
 36088             {
 36089               /* We must redraw a row overlapping the exposed area.  */
 36090               if (y0 < r.y
 36091                   ? y0 + row->phys_height > r.y
 36092                   : y0 + row->ascent - row->phys_ascent < r.y +r.height)
 36093                 {
 36094                   if (first_overlapping_row == NULL)
 36095                     first_overlapping_row = row;
 36096                   last_overlapping_row = row;
 36097                 }
 36098             }
 36099 
 36100           if (y1 >= yb)
 36101             break;
 36102         }
 36103 
 36104       if (buffer_changed)
 36105         set_buffer_internal_1 (oldbuf);
 36106 
 36107       /* Display the mode line if there is one.  */
 36108       if (window_wants_mode_line (w)
 36109           && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
 36110               row->enabled_p)
 36111           && row->y < r_bottom)
 36112         {
 36113           if (expose_line (w, row, &r))
 36114             mouse_face_overwritten_p = true;
 36115         }
 36116 
 36117       if (!w->pseudo_window_p)
 36118         {
 36119           /* Fix the display of overlapping rows.  */
 36120           if (first_overlapping_row)
 36121             expose_overlaps (w, first_overlapping_row, last_overlapping_row,
 36122                              fr);
 36123 
 36124           /* Draw border between windows.  */
 36125           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36126             gui_draw_right_divider (w);
 36127           else
 36128             gui_draw_vertical_border (w);
 36129 
 36130           if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36131             gui_draw_bottom_divider (w);
 36132 
 36133           /* Turn the cursor on again.  */
 36134           if (cursor_cleared_p
 36135               || (phys_cursor_on_p && !w->phys_cursor_on_p))
 36136             update_window_cursor (w, true);
 36137         }
 36138     }
 36139 
 36140   return mouse_face_overwritten_p;
 36141 }
 36142 
 36143 
 36144 
 36145 /* Redraw (parts) of all windows in the window tree rooted at W that
 36146    intersect R.  R contains frame pixel coordinates.  Value is
 36147    true if the exposure overwrites mouse-face.  */
 36148 
 36149 static bool
 36150 expose_window_tree (struct window *w, const Emacs_Rectangle *r)
 36151 {
 36152   struct frame *f = XFRAME (w->frame);
 36153   bool mouse_face_overwritten_p = false;
 36154 
 36155   while (w && !FRAME_GARBAGED_P (f))
 36156     {
 36157       mouse_face_overwritten_p
 36158         |= (WINDOWP (w->contents)
 36159             ? expose_window_tree (XWINDOW (w->contents), r)
 36160             : expose_window (w, r));
 36161 
 36162       w = NILP (w->next) ? NULL : XWINDOW (w->next);
 36163     }
 36164 
 36165   return mouse_face_overwritten_p;
 36166 }
 36167 
 36168 
 36169 /* EXPORT:
 36170    Redisplay an exposed area of frame F.  X and Y are the upper-left
 36171    corner of the exposed rectangle.  W and H are width and height of
 36172    the exposed area.  All are pixel values.  W or H zero means redraw
 36173    the entire frame.  */
 36174 
 36175 void
 36176 expose_frame (struct frame *f, int x, int y, int w, int h)
 36177 {
 36178   Emacs_Rectangle r;
 36179   bool mouse_face_overwritten_p = false;
 36180 
 36181   if (FRAME_GARBAGED_P (f))
 36182     {
 36183       redisplay_trace ("expose_frame garbaged\n");
 36184       return;
 36185     }
 36186 
 36187   /* If basic faces haven't been realized yet, there is no point in
 36188      trying to redraw anything.  This can happen when we get an expose
 36189      event while Emacs is starting, e.g. by moving another window.  */
 36190   if (FRAME_FACE_CACHE (f) == NULL
 36191       || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
 36192     {
 36193       redisplay_trace ("expose_frame no faces\n");
 36194       return;
 36195     }
 36196 
 36197   if (w == 0 || h == 0)
 36198     {
 36199       r.x = r.y = 0;
 36200       r.width = FRAME_TEXT_WIDTH (f);
 36201       r.height = FRAME_TEXT_HEIGHT (f);
 36202     }
 36203   else
 36204     {
 36205       r.x = x;
 36206       r.y = y;
 36207       r.width = w;
 36208       r.height = h;
 36209     }
 36210 
 36211   redisplay_trace ("expose_frame (%d, %d, %u, %u)\n",
 36212                    r.x, r.y, r.width, r.height);
 36213   mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
 36214 
 36215   if (WINDOWP (f->tab_bar_window))
 36216     mouse_face_overwritten_p
 36217       |= expose_window (XWINDOW (f->tab_bar_window), &r);
 36218 
 36219 #ifndef HAVE_EXT_TOOL_BAR
 36220   if (WINDOWP (f->tool_bar_window))
 36221     mouse_face_overwritten_p
 36222       |= expose_window (XWINDOW (f->tool_bar_window), &r);
 36223 #endif
 36224 
 36225 #ifdef HAVE_X_WINDOWS
 36226 #ifndef MSDOS
 36227 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
 36228   if (WINDOWP (f->menu_bar_window))
 36229     mouse_face_overwritten_p
 36230       |= expose_window (XWINDOW (f->menu_bar_window), &r);
 36231 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 36232 #endif
 36233 #endif
 36234 
 36235   /* Some window managers support a focus-follows-mouse style with
 36236      delayed raising of frames.  Imagine a partially obscured frame,
 36237      and moving the mouse into partially obscured mouse-face on that
 36238      frame.  The visible part of the mouse-face will be highlighted,
 36239      then the WM raises the obscured frame.  With at least one WM, KDE
 36240      2.1, Emacs is not getting any event for the raising of the frame
 36241      (even tried with SubstructureRedirectMask), only Expose events.
 36242      These expose events will draw text normally, i.e. not
 36243      highlighted.  Which means we must redo the highlight here.
 36244      Subsume it under ``we love X''.  --gerd 2001-08-15  */
 36245   /* Included in Windows version because Windows most likely does not
 36246      do the right thing if any third party tool offers
 36247      focus-follows-mouse with delayed raise.  --jason 2001-10-12  */
 36248   if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
 36249     {
 36250       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36251       if (f == hlinfo->mouse_face_mouse_frame)
 36252         {
 36253           int mouse_x = hlinfo->mouse_face_mouse_x;
 36254           int mouse_y = hlinfo->mouse_face_mouse_y;
 36255           clear_mouse_face (hlinfo);
 36256           note_mouse_highlight (f, mouse_x, mouse_y);
 36257         }
 36258     }
 36259 }
 36260 
 36261 
 36262 /* EXPORT:
 36263    Determine the intersection of two rectangles R1 and R2.  Return
 36264    the intersection in *RESULT.  Value is true if RESULT is not
 36265    empty.  */
 36266 
 36267 bool
 36268 gui_intersect_rectangles (const Emacs_Rectangle *r1, const Emacs_Rectangle *r2,
 36269                           Emacs_Rectangle *result)
 36270 {
 36271   const Emacs_Rectangle *left, *right;
 36272   const Emacs_Rectangle *upper, *lower;
 36273   bool intersection_p = false;
 36274 
 36275   /* Rearrange so that R1 is the left-most rectangle.  */
 36276   if (r1->x < r2->x)
 36277     left = r1, right = r2;
 36278   else
 36279     left = r2, right = r1;
 36280 
 36281   /* X0 of the intersection is right.x0, if this is inside R1,
 36282      otherwise there is no intersection.  */
 36283   if (right->x <= left->x + left->width)
 36284     {
 36285       result->x = right->x;
 36286 
 36287       /* The right end of the intersection is the minimum of
 36288          the right ends of left and right.  */
 36289       result->width = (min (left->x + left->width, right->x + right->width)
 36290                        - result->x);
 36291 
 36292       /* Same game for Y.  */
 36293       if (r1->y < r2->y)
 36294         upper = r1, lower = r2;
 36295       else
 36296         upper = r2, lower = r1;
 36297 
 36298       /* The upper end of the intersection is lower.y0, if this is inside
 36299          of upper.  Otherwise, there is no intersection.  */
 36300       if (lower->y <= upper->y + upper->height)
 36301         {
 36302           result->y = lower->y;
 36303 
 36304           /* The lower end of the intersection is the minimum of the lower
 36305              ends of upper and lower.  */
 36306           result->height = (min (lower->y + lower->height,
 36307                                  upper->y + upper->height)
 36308                             - result->y);
 36309           intersection_p = true;
 36310         }
 36311     }
 36312 
 36313   return intersection_p;
 36314 }
 36315 
 36316 #endif /* HAVE_WINDOW_SYSTEM */
 36317 
 36318 
 36319 /***********************************************************************
 36320                             Initialization
 36321  ***********************************************************************/
 36322 
 36323 void
 36324 syms_of_xdisp (void)
 36325 {
 36326   Vwith_echo_area_save_vector = Qnil;
 36327   staticpro (&Vwith_echo_area_save_vector);
 36328 
 36329   Vmessage_stack = Qnil;
 36330   staticpro (&Vmessage_stack);
 36331 
 36332   /* Non-nil means don't actually do any redisplay.  */
 36333   DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
 36334 
 36335   DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
 36336 
 36337   DEFVAR_BOOL ("scroll-minibuffer-conservatively",
 36338                scroll_minibuffer_conservatively,
 36339                doc: /* Non-nil means scroll conservatively in minibuffer windows.
 36340 When the value is nil, scrolling in minibuffer windows obeys the
 36341 settings of `scroll-conservatively'.  */);
 36342   scroll_minibuffer_conservatively = true; /* bug#44070 */
 36343 
 36344   DEFVAR_BOOL ("inhibit-message", inhibit_message,
 36345               doc:  /* Non-nil means calls to `message' are not displayed.
 36346 They are still logged to the *Messages* buffer.
 36347 
 36348 Do NOT set this globally to a non-nil value, as doing that will
 36349 disable messages everywhere, including in I-search and other
 36350 places where they are necessary.  This variable is intended to
 36351 be let-bound around code that needs to disable messages temporarily. */);
 36352   inhibit_message = false;
 36353 
 36354   message_dolog_marker1 = Fmake_marker ();
 36355   staticpro (&message_dolog_marker1);
 36356   message_dolog_marker2 = Fmake_marker ();
 36357   staticpro (&message_dolog_marker2);
 36358   message_dolog_marker3 = Fmake_marker ();
 36359   staticpro (&message_dolog_marker3);
 36360 
 36361   defsubr (&Sset_buffer_redisplay);
 36362 #ifdef GLYPH_DEBUG
 36363   defsubr (&Sdump_frame_glyph_matrix);
 36364   defsubr (&Sdump_glyph_matrix);
 36365   defsubr (&Sdump_glyph_row);
 36366   defsubr (&Sdump_tab_bar_row);
 36367   defsubr (&Sdump_tool_bar_row);
 36368   defsubr (&Strace_redisplay);
 36369   defsubr (&Strace_to_stderr);
 36370 #endif
 36371 #ifdef HAVE_WINDOW_SYSTEM
 36372   defsubr (&Stab_bar_height);
 36373   defsubr (&Stool_bar_height);
 36374   defsubr (&Slookup_image_map);
 36375 #endif
 36376   defsubr (&Sline_pixel_height);
 36377   defsubr (&Sformat_mode_line);
 36378   defsubr (&Sinvisible_p);
 36379   defsubr (&Scurrent_bidi_paragraph_direction);
 36380   defsubr (&Swindow_text_pixel_size);
 36381   defsubr (&Sbuffer_text_pixel_size);
 36382   defsubr (&Smove_point_visually);
 36383   defsubr (&Sbidi_find_overridden_directionality);
 36384   defsubr (&Sdisplay__line_is_continued_p);
 36385   defsubr (&Sget_display_property);
 36386   defsubr (&Slong_line_optimizations_p);
 36387 
 36388   DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
 36389   DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
 36390   DEFSYM (Qoverriding_local_map, "overriding-local-map");
 36391   DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
 36392   DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
 36393   DEFSYM (Qeval, "eval");
 36394   DEFSYM (QCdata, ":data");
 36395 
 36396   /* Names of text properties relevant for redisplay.  */
 36397   DEFSYM (Qdisplay, "display");
 36398   DEFSYM (Qspace_width, "space-width");
 36399   DEFSYM (Qraise, "raise");
 36400   DEFSYM (Qslice, "slice");
 36401   DEFSYM (Qspace, "space");
 36402   DEFSYM (Qmargin, "margin");
 36403   DEFSYM (Qpointer, "pointer");
 36404   DEFSYM (Qleft_margin, "left-margin");
 36405   DEFSYM (Qright_margin, "right-margin");
 36406   DEFSYM (Qcenter, "center");
 36407   DEFSYM (Qline_height, "line-height");
 36408   DEFSYM (QCalign_to, ":align-to");
 36409   DEFSYM (QCrelative_width, ":relative-width");
 36410   DEFSYM (QCrelative_height, ":relative-height");
 36411   DEFSYM (QCeval, ":eval");
 36412   DEFSYM (QCpropertize, ":propertize");
 36413   DEFSYM (QCfile, ":file");
 36414   DEFSYM (Qfontified, "fontified");
 36415   DEFSYM (Qfontification_functions, "fontification-functions");
 36416   DEFSYM (Qlong_line_optimizations_in_fontification_functions,
 36417           "long-line-optimizations-in-fontification-functions");
 36418 
 36419   /* Name of the symbol which disables Lisp evaluation in 'display'
 36420      properties.  This is used by enriched.el.  */
 36421   DEFSYM (Qdisable_eval, "disable-eval");
 36422 
 36423   /* Name of the face used to highlight trailing whitespace.  */
 36424   DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
 36425 
 36426   /* Names of the faces used to display line numbers.  */
 36427   DEFSYM (Qline_number, "line-number");
 36428   DEFSYM (Qline_number_current_line, "line-number-current-line");
 36429   DEFSYM (Qline_number_major_tick, "line-number-major-tick");
 36430   DEFSYM (Qline_number_minor_tick, "line-number-minor-tick");
 36431   /* Name of a text property which disables line-number display.  */
 36432   DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
 36433 
 36434   /* Name of the face used to display fill column indicator character.  */
 36435   DEFSYM (Qfill_column_indicator, "fill-column-indicator");
 36436 
 36437   /* Name and number of the face used to highlight escape glyphs.  */
 36438   DEFSYM (Qescape_glyph, "escape-glyph");
 36439 
 36440   /* Name and number of the face used to highlight non-breaking
 36441      spaces/hyphens.  */
 36442   DEFSYM (Qnobreak_space, "nobreak-space");
 36443   DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
 36444 
 36445   /* The symbol 'image' which is the car of the lists used to represent
 36446      images in Lisp.  Also a tool bar style.  */
 36447   DEFSYM (Qimage, "image");
 36448 
 36449   /* Tool bar styles.  */
 36450   DEFSYM (Qtext, "text");
 36451   DEFSYM (Qboth, "both");
 36452   DEFSYM (Qboth_horiz, "both-horiz");
 36453   DEFSYM (Qtext_image_horiz, "text-image-horiz");
 36454 
 36455   /* The image map types.  */
 36456   DEFSYM (QCmap, ":map");
 36457   DEFSYM (QCpointer, ":pointer");
 36458   DEFSYM (Qrect, "rect");
 36459   DEFSYM (Qcircle, "circle");
 36460   DEFSYM (Qpoly, "poly");
 36461 
 36462   DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
 36463 
 36464   DEFSYM (Qgrow_only, "grow-only");
 36465   DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
 36466   DEFSYM (Qposition, "position");
 36467   DEFSYM (Qbuffer_position, "buffer-position");
 36468   DEFSYM (Qobject, "object");
 36469 
 36470   /* Cursor shapes.  */
 36471   DEFSYM (Qbar, "bar");
 36472   DEFSYM (Qhbar, "hbar");
 36473   DEFSYM (Qbox, "box");
 36474   DEFSYM (Qhollow, "hollow");
 36475 
 36476   /* Pointer shapes.  */
 36477   DEFSYM (Qhand, "hand");
 36478   DEFSYM (Qarrow, "arrow");
 36479   /* also Qtext */
 36480 
 36481   DEFSYM (Qdragging, "dragging");
 36482   DEFSYM (Qdropping, "dropping");
 36483   DEFSYM (Qdrag_source, "drag-source");
 36484 
 36485   DEFSYM (Qdrag_with_mode_line, "drag-with-mode-line");
 36486   DEFSYM (Qdrag_with_header_line, "drag-with-header-line");
 36487   DEFSYM (Qdrag_with_tab_line, "drag-with-tab-line");
 36488 
 36489   DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
 36490 
 36491   list_of_error = list1 (Qerror);
 36492   staticpro (&list_of_error);
 36493 
 36494   /* Values of those variables at last redisplay are stored as
 36495      properties on 'overlay-arrow-position' symbol.  However, if
 36496      Voverlay_arrow_position is a marker, last-arrow-position is its
 36497      numerical position.  */
 36498   DEFSYM (Qlast_arrow_position, "last-arrow-position");
 36499   DEFSYM (Qlast_arrow_string, "last-arrow-string");
 36500 
 36501   /* Alternative overlay-arrow-string and overlay-arrow-bitmap
 36502      properties on a symbol in overlay-arrow-variable-list.  */
 36503   DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
 36504   DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
 36505 
 36506   echo_buffer[0] = echo_buffer[1] = Qnil;
 36507   staticpro (&echo_buffer[0]);
 36508   staticpro (&echo_buffer[1]);
 36509 
 36510   echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
 36511   staticpro (&echo_area_buffer[0]);
 36512   staticpro (&echo_area_buffer[1]);
 36513 
 36514   DEFVAR_LISP ("messages-buffer-name", Vmessages_buffer_name,
 36515     doc: /* The name of the buffer where messages are logged.
 36516 This is normally \"\*Messages*\", but can be rebound by packages that
 36517 wish to redirect messages to a different buffer.  (If the buffer
 36518 doesn't exist, it will be created and put into
 36519 `messages-buffer-mode'.)  */);
 36520   Vmessages_buffer_name = build_string ("*Messages*");
 36521 
 36522   mode_line_proptrans_alist = Qnil;
 36523   staticpro (&mode_line_proptrans_alist);
 36524   mode_line_string_list = Qnil;
 36525   staticpro (&mode_line_string_list);
 36526   mode_line_string_face = Qnil;
 36527   staticpro (&mode_line_string_face);
 36528   mode_line_string_face_prop = Qnil;
 36529   staticpro (&mode_line_string_face_prop);
 36530   Vmode_line_unwind_vector = Qnil;
 36531   staticpro (&Vmode_line_unwind_vector);
 36532 
 36533   DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
 36534 
 36535   help_echo_string = Qnil;
 36536   staticpro (&help_echo_string);
 36537   help_echo_object = Qnil;
 36538   staticpro (&help_echo_object);
 36539   help_echo_window = Qnil;
 36540   staticpro (&help_echo_window);
 36541   previous_help_echo_string = Qnil;
 36542   staticpro (&previous_help_echo_string);
 36543   help_echo_pos = -1;
 36544 
 36545   DEFSYM (Qright_to_left, "right-to-left");
 36546   DEFSYM (Qleft_to_right, "left-to-right");
 36547   defsubr (&Sbidi_resolved_levels);
 36548 
 36549 #ifdef HAVE_WINDOW_SYSTEM
 36550   DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
 36551     doc: /* Non-nil means draw block cursor as wide as the glyph under it.
 36552 For example, if a block cursor is over a tab, it will be drawn as
 36553 wide as that tab on the display.  */);
 36554   x_stretch_cursor_p = 0;
 36555 #endif
 36556 
 36557   DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
 36558     doc: /* Non-nil means highlight trailing whitespace.
 36559 The face used for trailing whitespace is `trailing-whitespace'.  */);
 36560   Vshow_trailing_whitespace = Qnil;
 36561 
 36562   DEFVAR_LISP ("mode-line-compact", Vmode_line_compact,
 36563     doc: /* Non-nil means that mode lines should be compact.
 36564 This means that repeating spaces will be replaced with a single space.
 36565 If this variable is `long', only mode lines that are wider than the
 36566 currently selected window are compressed. */);
 36567   Vmode_line_compact = Qnil;
 36568   DEFSYM (Qlong, "long");
 36569 
 36570   DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
 36571     doc: /* Control highlighting of non-ASCII space and hyphen chars.
 36572 If the value is t, Emacs highlights non-ASCII chars which have the
 36573 same appearance as an ASCII space or hyphen, using the `nobreak-space'
 36574 or `nobreak-hyphen' face respectively.
 36575 
 36576 All of the non-ASCII characters in the Unicode horizontal whitespace
 36577 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36578 U+2011 (non-breaking hyphen) are affected.
 36579 
 36580 Any other non-nil value means to display these characters as an escape
 36581 glyph followed by an ordinary space or hyphen.
 36582 
 36583 A value of nil means no special handling of these characters.  */);
 36584   Vnobreak_char_display = Qt;
 36585 
 36586   DEFVAR_BOOL ("nobreak-char-ascii-display", nobreak_char_ascii_display,
 36587     doc: /* Control display of non-ASCII space and hyphen chars.
 36588 If the value of this variable is nil, the default, Emacs displays
 36589 non-ASCII chars which have the same appearance as an ASCII space
 36590 or hyphen as themselves, with the `nobreak-space' or `nobreak-hyphen'
 36591 face, respectively.
 36592 
 36593 If the value is t, these characters are displayed as their ASCII
 36594 counterparts: whitespace characters as ASCII space, hyphen characters
 36595 as ASCII hyphen (a.k.a. \"dash\"), using the `nobreak-space' or
 36596 the `nobreak-hyphen' face.
 36597 
 36598 This variable has effect only if `nobreak-char-display' is t;
 36599 otherwise it is ignored.
 36600 
 36601 All of the non-ASCII characters in the Unicode horizontal whitespace
 36602 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36603 U+2011 (non-breaking hyphen) are affected.  */);
 36604   nobreak_char_ascii_display = false;
 36605 
 36606   DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
 36607     doc: /* The pointer shape to show in void text areas.
 36608 A value of nil means to show the text pointer.  Other options are
 36609 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
 36610 `hourglass'.  */);
 36611   Vvoid_text_area_pointer = Qarrow;
 36612 
 36613   DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
 36614     doc: /* Non-nil means don't actually do any redisplay.
 36615 This is used for internal purposes.  */);
 36616   Vinhibit_redisplay = Qnil;
 36617 
 36618   DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
 36619     doc: /* String (or mode line construct) included (normally) in `mode-line-misc-info'.  */);
 36620   Vglobal_mode_string = Qnil;
 36621 
 36622   DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
 36623     doc: /* Marker for where to display an arrow on top of the buffer text.
 36624 This must be the beginning of a line in order to work.
 36625 See also `overlay-arrow-string'.  */);
 36626   Voverlay_arrow_position = Qnil;
 36627 
 36628   DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
 36629     doc: /* String to display as an arrow in non-window frames.
 36630 See also `overlay-arrow-position'.  */);
 36631   Voverlay_arrow_string = build_pure_c_string ("=>");
 36632 
 36633   DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
 36634     doc: /* List of variables (symbols) which hold markers for overlay arrows.
 36635 The symbols on this list are examined during redisplay to determine
 36636 where to display overlay arrows.  */);
 36637   Voverlay_arrow_variable_list
 36638     = list1 (intern_c_string ("overlay-arrow-position"));
 36639 
 36640   DEFVAR_INT ("scroll-step", emacs_scroll_step,
 36641     doc: /* The number of lines to try scrolling a window by when point moves out.
 36642 If that fails to bring point back on frame, point is centered instead.
 36643 If this is zero, point is always centered after it moves off frame.
 36644 If you want scrolling to always be a line at a time, you should set
 36645 `scroll-conservatively' to a large value rather than set this to 1.  */);
 36646 
 36647   DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
 36648     doc: /* Scroll up to this many lines, to bring point back on screen.
 36649 If point moves off-screen, redisplay will scroll by up to
 36650 `scroll-conservatively' lines in order to bring point just barely
 36651 onto the screen again.  If that cannot be done, then redisplay
 36652 recenters point as usual.
 36653 
 36654 If the value is greater than 100, redisplay will never recenter point,
 36655 but will always scroll just enough text to bring point into view, even
 36656 if you move far away.
 36657 
 36658 A value of zero means always recenter point if it moves off screen.  */);
 36659   scroll_conservatively = 0;
 36660 
 36661   DEFVAR_INT ("scroll-margin", scroll_margin,
 36662     doc: /* Number of lines of margin at the top and bottom of a window.
 36663 Trigger automatic scrolling whenever point gets within this many lines
 36664 of the top or bottom of the window (see info node `Auto Scrolling').  */);
 36665   scroll_margin = 0;
 36666 
 36667   DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
 36668     doc: /* Maximum effective value of `scroll-margin'.
 36669 Given as a fraction of the current window's lines.  The value should
 36670 be a floating point number between 0.0 and 0.5.  The effective maximum
 36671 is limited to (/ (1- window-lines) 2).  Non-float values for this
 36672 variable are ignored and the default 0.25 is used instead.  */);
 36673   Vmaximum_scroll_margin = make_float (0.25);
 36674 
 36675   DEFVAR_LISP ("display-pixels-per-inch",  Vdisplay_pixels_per_inch,
 36676     doc: /* Pixels per inch value for non-window system displays.
 36677 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI).  */);
 36678   Vdisplay_pixels_per_inch = make_float (72.0);
 36679 
 36680 #ifdef GLYPH_DEBUG
 36681   DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask.  */);
 36682 #endif
 36683 
 36684   DEFVAR_LISP ("truncate-partial-width-windows",
 36685                Vtruncate_partial_width_windows,
 36686     doc: /* Non-nil means truncate lines in windows narrower than the frame.
 36687 For an integer value, truncate lines in each window narrower than the
 36688 full frame width, provided the total window width in column units is less
 36689 than that integer; otherwise, respect the value of `truncate-lines'.
 36690 The total width of the window is as returned by `window-total-width', it
 36691 includes the fringes, the continuation and truncation glyphs, the
 36692 display margins (if any), and the scroll bar
 36693 
 36694 For any other non-nil value, truncate lines in all windows that do
 36695 not span the full frame width.
 36696 
 36697 A value of nil means to respect the value of `truncate-lines'.
 36698 
 36699 If `word-wrap' is enabled, you might want to reduce the value of this.
 36700 
 36701 Don't set this to a non-nil value when `visual-line-mode' is
 36702 turned on, as it could produce confusing results.  */);
 36703   Vtruncate_partial_width_windows = make_fixnum (50);
 36704 
 36705   DEFVAR_BOOL("word-wrap-by-category", word_wrap_by_category, doc: /*
 36706     Non-nil means also wrap after characters of a certain category.
 36707 Normally when `word-wrap' is on, Emacs only breaks lines after
 36708 whitespace characters.  When this option is turned on, Emacs also
 36709 breaks lines after characters that have the "|" category (defined in
 36710 characters.el).  This is useful for allowing breaking after CJK
 36711 characters and improves the word-wrapping for CJK text mixed with
 36712 Latin text.
 36713 
 36714 If this variable is set using Customize, Emacs automatically loads
 36715 kinsoku.el.  When kinsoku.el is loaded, Emacs respects kinsoku rules
 36716 when breaking lines.  That means characters with the ">" category
 36717 don't appear at the beginning of a line (e.g., FULLWIDTH COMMA), and
 36718 characters with the "<" category don't appear at the end of a line
 36719 (e.g., LEFT DOUBLE ANGLE BRACKET).  */);
 36720   word_wrap_by_category = false;
 36721 
 36722   DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
 36723     doc: /* Maximum buffer size for which line number should be displayed.
 36724 If the buffer is bigger than this, the line number does not appear
 36725 in the mode line.  A value of nil means no limit.  */);
 36726   Vline_number_display_limit = Qnil;
 36727 
 36728   DEFVAR_INT ("line-number-display-limit-width",
 36729               line_number_display_limit_width,
 36730     doc: /* Maximum line width (in characters) for line number display.
 36731 If the average length of the lines near point is bigger than this, then the
 36732 line number may be omitted from the mode line.  */);
 36733   line_number_display_limit_width = 200;
 36734 
 36735   DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
 36736     doc: /* Non-nil means highlight active region even in nonselected windows.
 36737 When nil (the default), the active region is only highlighted when
 36738 the window is selected.  */);
 36739   highlight_nonselected_windows = false;
 36740 
 36741   DEFVAR_BOOL ("multiple-frames", multiple_frames,
 36742     doc: /* Non-nil if more than one frame is visible on this display.
 36743 Minibuffer-only frames don't count, but iconified frames do.
 36744 This variable is not guaranteed to be accurate except while processing
 36745 `frame-title-format' and `icon-title-format'.  */);
 36746 
 36747   DEFVAR_LISP ("frame-title-format", Vframe_title_format,
 36748     doc: /* Template for displaying the title bar of visible frames.
 36749 \(Assuming the window manager supports this feature.)
 36750 
 36751 This variable has the same structure as `mode-line-format', except that
 36752 the %c, %C, and %l constructs are ignored.  It is used only on frames for
 36753 which no explicit name has been set (see `modify-frame-parameters').  */);
 36754 
 36755   DEFVAR_LISP ("icon-title-format", Vicon_title_format,
 36756     doc: /* Template for displaying the title bar of an iconified frame.
 36757 \(Assuming the window manager supports this feature.)
 36758 If the value is a string, it should have the same structure
 36759 as `mode-line-format' (which see), and is used only on frames
 36760 for which no explicit name has been set \(see `modify-frame-parameters').
 36761 If the value is t, that means use `frame-title-format' for
 36762 iconified frames.  */);
 36763   /* Do not nest calls to pure_list.  This works around a bug in
 36764      Oracle Developer Studio 12.6.  */
 36765   Lisp_Object icon_title_name_format
 36766     = pure_list (empty_unibyte_string,
 36767                  build_pure_c_string ("%b - GNU Emacs at "),
 36768                  intern_c_string ("system-name"));
 36769   Vicon_title_format
 36770     = Vframe_title_format
 36771     = pure_list (intern_c_string ("multiple-frames"),
 36772                  build_pure_c_string ("%b"),
 36773                  icon_title_name_format);
 36774 
 36775   DEFVAR_LISP ("message-log-max", Vmessage_log_max,
 36776     doc: /* Maximum number of lines to keep in the message log buffer.
 36777 If nil, disable message logging.  If t, log messages but don't truncate
 36778 the buffer when it becomes large.  */);
 36779   Vmessage_log_max = make_fixnum (1000);
 36780 
 36781   DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
 36782     doc: /* List of functions to call before redisplaying a window with scrolling.
 36783 Each function is called with two arguments, the window and its new
 36784 display-start position.
 36785 These functions are called whenever the `window-start' marker is modified,
 36786 either to point into another buffer (e.g. via `set-window-buffer') or another
 36787 place in the same buffer.
 36788 When each function is called, the `window-start' marker of its window
 36789 argument has been already set to the new value, and the buffer which that
 36790 window will display is set to be the current buffer.
 36791 Note that the value of `window-end' is not valid when these functions are
 36792 called.
 36793 
 36794 Warning: Do not use this feature to alter the way the window
 36795 is scrolled.  It is not designed for that, and such use probably won't
 36796 work.  */);
 36797   Vwindow_scroll_functions = Qnil;
 36798 
 36799   DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
 36800      doc: /* Non-nil means autoselect window with mouse pointer.
 36801 If nil, do not autoselect windows.
 36802 A positive number means delay autoselection by that many seconds: a
 36803 window is autoselected only after the mouse has remained in that
 36804 window for the duration of the delay.
 36805 A negative number has a similar effect, but causes windows to be
 36806 autoselected only after the mouse has stopped moving.  (Because of
 36807 the way Emacs compares mouse events, you will occasionally wait twice
 36808 that time before the window gets selected.)
 36809 Any other value means to autoselect window instantaneously when the
 36810 mouse pointer enters it.
 36811 
 36812 Autoselection selects the minibuffer only if it is active, and never
 36813 unselects the minibuffer if it is active.
 36814 
 36815 If you want to use the mouse to autoselect a window on another frame,
 36816 make sure that (1) your window manager has focus follow the mouse and
 36817 (2) the value of the option `focus-follows-mouse' matches the policy
 36818 of your window manager.  */);
 36819   Vmouse_autoselect_window = Qnil;
 36820 
 36821   DEFVAR_LISP ("auto-resize-tab-bars", Vauto_resize_tab_bars,
 36822     doc: /* Non-nil means automatically resize tab-bars.
 36823 This dynamically changes the tab-bar's height to the minimum height
 36824 that is needed to make all tab-bar items visible.
 36825 If value is `grow-only', the tab-bar's height is only increased
 36826 automatically; to decrease the tab-bar height, use \\[recenter],
 36827 after setting `recenter-redisplay' to the value of t.  */);
 36828   Vauto_resize_tab_bars = Qt;
 36829 
 36830   DEFVAR_BOOL ("auto-raise-tab-bar-buttons", auto_raise_tab_bar_buttons_p,
 36831     doc: /* Non-nil means raise tab-bar buttons when the mouse moves over them.  */);
 36832   auto_raise_tab_bar_buttons_p = true;
 36833 
 36834   DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
 36835     doc: /* Non-nil means automatically resize tool-bars.
 36836 This dynamically changes the tool-bar's height to the minimum height
 36837 that is needed to make all tool-bar items visible.
 36838 If value is `grow-only', the tool-bar's height is only increased
 36839 automatically; to decrease the tool-bar height, use \\[recenter],
 36840 after setting `recenter-redisplay' to the value of t.  */);
 36841   Vauto_resize_tool_bars = Qt;
 36842 
 36843   DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
 36844     doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them.  */);
 36845   auto_raise_tool_bar_buttons_p = true;
 36846 
 36847   DEFVAR_LISP ("make-cursor-line-fully-visible", Vmake_cursor_line_fully_visible,
 36848     doc: /* Whether to scroll the window if the cursor line is not fully visible.
 36849 If the value is non-nil, Emacs scrolls or recenters the window to make
 36850 the cursor line fully visible.  The value could also be a function, which
 36851 is called with a single argument, the window to be scrolled, and should
 36852 return non-nil if the partially-visible cursor requires scrolling the
 36853 window, nil if it's okay to leave the cursor partially-visible.  */);
 36854   Vmake_cursor_line_fully_visible = Qt;
 36855   DEFSYM (Qmake_cursor_line_fully_visible, "make-cursor-line-fully-visible");
 36856 
 36857   DEFVAR_BOOL ("make-window-start-visible", make_window_start_visible,
 36858     doc: /* Whether to ensure `window-start' position is never invisible.  */);
 36859   make_window_start_visible = false;
 36860   DEFSYM (Qmake_window_start_visible, "make-window-start-visible");
 36861   Fmake_variable_buffer_local (Qmake_window_start_visible);
 36862 
 36863   DEFSYM (Qclose_tab, "close-tab");
 36864   DEFVAR_LISP ("tab-bar-border", Vtab_bar_border,
 36865     doc: /* Border below tab-bar in pixels.
 36866 If an integer, use it as the height of the border.
 36867 If it is one of `internal-border-width' or `border-width', use the
 36868 value of the corresponding frame parameter.
 36869 Otherwise, no border is added below the tab-bar.  */);
 36870   Vtab_bar_border = Qinternal_border_width;
 36871 
 36872   DEFVAR_LISP ("tab-bar-button-margin", Vtab_bar_button_margin,
 36873     doc: /* Margin around tab-bar buttons in pixels.
 36874 If an integer, use that for both horizontal and vertical margins.
 36875 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 36876 HORZ specifying the horizontal margin, and VERT specifying the
 36877 vertical margin.  */);
 36878   Vtab_bar_button_margin = make_fixnum (DEFAULT_TAB_BAR_BUTTON_MARGIN);
 36879 
 36880   DEFVAR_INT ("tab-bar-button-relief", tab_bar_button_relief,
 36881     doc: /* Relief thickness of tab-bar buttons.  */);
 36882   tab_bar_button_relief = DEFAULT_TAB_BAR_BUTTON_RELIEF;
 36883 
 36884   DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
 36885     doc: /* Border below tool-bar in pixels.
 36886 If an integer, use it as the height of the border.
 36887 If it is one of `internal-border-width' or `border-width', use the
 36888 value of the corresponding frame parameter.
 36889 Otherwise, no border is added below the tool-bar.  */);
 36890   Vtool_bar_border = Qinternal_border_width;
 36891 
 36892   DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
 36893     doc: /* Margin around tool-bar buttons in pixels.
 36894 If an integer, use that for both horizontal and vertical margins.
 36895 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 36896 HORZ specifying the horizontal margin, and VERT specifying the
 36897 vertical margin.  */);
 36898   Vtool_bar_button_margin = make_fixnum (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
 36899 
 36900   DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
 36901     doc: /* Relief thickness of tool-bar buttons.  */);
 36902   tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
 36903 
 36904   DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
 36905     doc: /* Tool bar style to use.
 36906 It can be one of
 36907  image            - show images only
 36908  text             - show text only
 36909  both             - show both, text below image
 36910  both-horiz       - show text to the right of the image
 36911  text-image-horiz - show text to the left of the image
 36912  any other        - use system default or image if no system default.
 36913 
 36914 This variable only affects the GTK+ toolkit version of Emacs.  */);
 36915   Vtool_bar_style = Qnil;
 36916 
 36917   DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
 36918     doc: /* Maximum number of characters a label can have to be shown.
 36919 The tool bar style must also show labels for this to have any effect, see
 36920 `tool-bar-style'.  */);
 36921   tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
 36922 
 36923   DEFVAR_LISP ("fontification-functions", Vfontification_functions,
 36924     doc: /* List of functions to call to fontify regions of text.
 36925 Each function is called with one argument POS.  Functions must
 36926 fontify a region starting at POS in the current buffer, and give
 36927 fontified regions the property `fontified' with a non-nil value.
 36928 
 36929 Note that, when `long-line-optimizations-p' is non-nil in the buffer,
 36930 these functions are called as if they were in a `with-restriction' form,
 36931 with a `long-line-optimizations-in-fontification-functions' label and
 36932 with the buffer narrowed to a portion around POS whose size is
 36933 specified by `long-line-optimizations-region-size'.  */);
 36934   Vfontification_functions = Qnil;
 36935   Fmake_variable_buffer_local (Qfontification_functions);
 36936 
 36937   DEFVAR_BOOL ("unibyte-display-via-language-environment",
 36938                unibyte_display_via_language_environment,
 36939     doc: /* Non-nil means display unibyte text according to language environment.
 36940 Specifically, this means that raw bytes in the range 160-255 decimal
 36941 are displayed by converting them to the equivalent multibyte characters
 36942 according to the current language environment.  As a result, they are
 36943 displayed according to the current fontset.
 36944 
 36945 Note that this variable affects only how these bytes are displayed,
 36946 but does not change the fact they are interpreted as raw bytes.  */);
 36947   unibyte_display_via_language_environment = false;
 36948 
 36949   DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
 36950     doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
 36951 If a float, it specifies the maximum height in units of the
 36952 mini-window frame's height.
 36953 If an integer, it specifies the maximum height in units of the
 36954 mini-window frame's default font's height.  */);
 36955   Vmax_mini_window_height = make_float (0.25);
 36956 
 36957   DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
 36958     doc: /* How to resize mini-windows (the minibuffer and the echo area).
 36959 A value of nil means don't automatically resize mini-windows.
 36960 A value of t means resize them to fit the text displayed in them.
 36961 A value of `grow-only', the default, means let mini-windows grow only;
 36962 they return to their normal size when the minibuffer is closed, or the
 36963 echo area becomes empty.
 36964 
 36965 This variable does not affect resizing of the minibuffer window of
 36966 minibuffer-only frames.  These are handled by `resize-mini-frames'
 36967 only.  */);
 36968   /* Contrary to the doc string, we initialize this to nil, so that
 36969      loading loadup.el won't try to resize windows before loading
 36970      window.el, where some functions we need to call for this live.
 36971      We assign the 'grow-only' value right after loading window.el
 36972      during loadup.  */
 36973   Vresize_mini_windows = Qnil;
 36974 
 36975   DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
 36976     doc: /* Alist specifying how to blink the cursor off.
 36977 Each element has the form (ON-STATE . OFF-STATE).  Whenever the
 36978 `cursor-type' frame-parameter or variable equals ON-STATE,
 36979 comparing using `equal', Emacs uses OFF-STATE to specify
 36980 how to blink it off.  ON-STATE and OFF-STATE are values for
 36981 the `cursor-type' frame parameter.
 36982 
 36983 If a frame's ON-STATE has no entry in this list,
 36984 the frame's other specifications determine how to blink the cursor off.  */);
 36985   Vblink_cursor_alist = Qnil;
 36986 
 36987   DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
 36988     doc: /* Allow or disallow automatic horizontal scrolling of windows.
 36989 The value `current-line' means the line displaying point in each window
 36990 is automatically scrolled horizontally to make point visible.
 36991 Any other non-nil value means all the lines in a window are automatically
 36992 scrolled horizontally to make point visible.  */);
 36993   automatic_hscrolling = Qt;
 36994   DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
 36995   DEFSYM (Qcurrent_line, "current-line");
 36996 
 36997   DEFVAR_INT ("hscroll-margin", hscroll_margin,
 36998     doc: /* How many columns away from the window edge point is allowed to get
 36999 before automatic hscrolling will horizontally scroll the window.  */);
 37000   hscroll_margin = 5;
 37001 
 37002   DEFVAR_LISP ("hscroll-step", Vhscroll_step,
 37003     doc: /* How many columns to scroll the window when point gets too close to the edge.
 37004 When point is less than `hscroll-margin' columns from the window
 37005 edge, automatic hscrolling will scroll the window by the amount of columns
 37006 determined by this variable.  If its value is a positive integer, scroll that
 37007 many columns.  If it's a positive floating-point number, it specifies the
 37008 fraction of the window's width to scroll.  If it's nil or zero, point will be
 37009 centered horizontally after the scroll.  Any other value, including negative
 37010 numbers, are treated as if the value were zero.
 37011 
 37012 Automatic hscrolling always moves point outside the scroll margin, so if
 37013 point was more than scroll step columns inside the margin, the window will
 37014 scroll more than the value given by the scroll step.
 37015 
 37016 Note that the lower bound for automatic hscrolling specified by `scroll-left'
 37017 and `scroll-right' overrides this variable's effect.  */);
 37018   Vhscroll_step = make_fixnum (0);
 37019 
 37020   DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
 37021     doc: /* If non-nil, messages are truncated when displaying the echo area.
 37022 Bind this around calls to `message' to let it take effect.  */);
 37023   message_truncate_lines = false;
 37024 
 37025   DEFVAR_LISP ("menu-bar-update-hook",  Vmenu_bar_update_hook,
 37026     doc: /* Normal hook run to update the menu bar definitions.
 37027 Redisplay runs this hook before it redisplays the menu bar.
 37028 This is used to update menus such as Buffers, whose contents depend on
 37029 various data.  */);
 37030   Vmenu_bar_update_hook = Qnil;
 37031 
 37032   DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
 37033                doc: /* Frame for which we are updating a menu.
 37034 The enable predicate for a menu binding should check this variable.  */);
 37035   Vmenu_updating_frame = Qnil;
 37036 
 37037   DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
 37038     doc: /* Non-nil means don't update menu bars.  Internal use only.  */);
 37039   inhibit_menubar_update = false;
 37040 
 37041   DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
 37042     doc: /* Prefix prepended to all continuation lines at display time.
 37043 The value may be a string, an image, or a stretch-glyph; it is
 37044 interpreted in the same way as the value of a `display' text property.
 37045 
 37046 This variable is overridden by any `wrap-prefix' text or overlay
 37047 property.
 37048 
 37049 To add a prefix to non-continuation lines, use `line-prefix'.  */);
 37050   Vwrap_prefix = Qnil;
 37051   DEFSYM (Qwrap_prefix, "wrap-prefix");
 37052   Fmake_variable_buffer_local (Qwrap_prefix);
 37053 
 37054   DEFVAR_LISP ("line-prefix", Vline_prefix,
 37055     doc: /* Prefix prepended to all non-continuation lines at display time.
 37056 The value may be a string, an image, or a stretch-glyph; it is
 37057 interpreted in the same way as the value of a `display' text property.
 37058 
 37059 This variable is overridden by any `line-prefix' text or overlay
 37060 property.
 37061 
 37062 To add a prefix to continuation lines, use `wrap-prefix'.  */);
 37063   Vline_prefix = Qnil;
 37064   DEFSYM (Qline_prefix, "line-prefix");
 37065   Fmake_variable_buffer_local (Qline_prefix);
 37066 
 37067   DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
 37068     doc: /* Non-nil means display line numbers.
 37069 
 37070 If the value is t, display the absolute number of each line of a buffer
 37071 shown in a window.  Absolute line numbers count from the beginning of
 37072 the current narrowing, or from buffer beginning.  The variable
 37073 `display-line-numbers-offset', if non-zero, is a signed offset added
 37074 to each absolute line number; it also forces line numbers to be counted
 37075 from the beginning of the buffer, as if `display-line-numbers-widen'
 37076 were non-nil.  It has no effect when line numbers are not absolute.
 37077 
 37078 If the value is `relative', display for each line not containing the
 37079 window's point its relative number instead, i.e. the number of the line
 37080 relative to the line showing the window's point.
 37081 
 37082 In either case, line numbers are displayed at the beginning of each
 37083 non-continuation line that displays buffer text, i.e. after each newline
 37084 character that comes from the buffer.  The value `visual' is like
 37085 `relative' but counts screen lines instead of buffer lines.  In practice
 37086 this means that continuation lines count as well when calculating the
 37087 relative number of a line.
 37088 
 37089 Lisp programs can disable display of a line number of a particular
 37090 buffer line by putting the `display-line-numbers-disable' text property
 37091 or overlay property on the first visible character of that line.  */);
 37092   Vdisplay_line_numbers = Qnil;
 37093   DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
 37094   Fmake_variable_buffer_local (Qdisplay_line_numbers);
 37095   DEFSYM (Qrelative, "relative");
 37096   DEFSYM (Qvisual, "visual");
 37097 
 37098   DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
 37099     doc: /* Minimum width of space reserved for line number display.
 37100 A positive number means reserve that many columns for line numbers,
 37101 even if the actual number needs less space.
 37102 The default value of nil means compute the space dynamically.
 37103 Any other value is treated as nil.  */);
 37104   Vdisplay_line_numbers_width = Qnil;
 37105   DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
 37106   Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
 37107 
 37108   DEFVAR_LISP ("display-line-numbers-current-absolute",
 37109                Vdisplay_line_numbers_current_absolute,
 37110     doc: /* Non-nil means display absolute number of current line.
 37111 This variable has effect only when `display-line-numbers' is
 37112 either `relative' or `visual'.  */);
 37113   Vdisplay_line_numbers_current_absolute = Qt;
 37114 
 37115   DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
 37116     doc: /* Non-nil means display line numbers disregarding any narrowing.  */);
 37117   display_line_numbers_widen = false;
 37118   DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
 37119   Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
 37120 
 37121   DEFVAR_INT ("display-line-numbers-offset", display_line_numbers_offset,
 37122     doc: /* A signed integer added to each absolute line number.
 37123 When this variable is non-zero, line numbers are always counted from
 37124 the beginning of the buffer even if `display-line-numbers-widen' is nil.
 37125 It has no effect when set to 0, or when line numbers are not absolute.  */);
 37126   display_line_numbers_offset = 0;
 37127   DEFSYM (Qdisplay_line_numbers_offset, "display-line-numbers-offset");
 37128   Fmake_variable_buffer_local (Qdisplay_line_numbers_offset);
 37129 
 37130   DEFVAR_BOOL ("display-fill-column-indicator", display_fill_column_indicator,
 37131     doc: /* Non-nil means display the fill column indicator.
 37132 If you set this non-nil, make sure `display-fill-column-indicator-character'
 37133 is also non-nil.
 37134 See Info node `Displaying Boundaries' for details.  */);
 37135   display_fill_column_indicator = false;
 37136   DEFSYM (Qdisplay_fill_column_indicator, "display-fill-column-indicator");
 37137   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator);
 37138 
 37139   DEFVAR_LISP ("display-fill-column-indicator-column", Vdisplay_fill_column_indicator_column,
 37140     doc: /* Column for indicator when `display-fill-column-indicator' is non-nil.
 37141 The default value is t which means that the indicator
 37142 will use the `fill-column' variable.  If it is set to an integer the
 37143 indicator will be drawn in that column.
 37144 See Info node `Displaying Boundaries' for details.  */);
 37145   Vdisplay_fill_column_indicator_column = Qt;
 37146   DEFSYM (Qdisplay_fill_column_indicator_column, "display-fill-column-indicator-column");
 37147   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_column);
 37148 
 37149   DEFVAR_LISP ("display-fill-column-indicator-character", Vdisplay_fill_column_indicator_character,
 37150     doc: /* Character to draw the indicator when `display-fill-column-indicator' is non-nil.
 37151 A good candidate is U+2502, and an alternative is (ascii 124) if the
 37152 font of `fill-column-indicator' face does not support Unicode characters.
 37153 See Info node `Displaying Boundaries' for details.  */);
 37154   Vdisplay_fill_column_indicator_character = Qnil;
 37155   DEFSYM (Qdisplay_fill_column_indicator_character, "display-fill-column-indicator-character");
 37156   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_character);
 37157 
 37158   DEFVAR_INT ("display-line-numbers-major-tick", display_line_numbers_major_tick,
 37159     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37160 The line number is shown with the `line-number-major-tick' face.
 37161 Otherwise, no special highlighting is done every Nth line.
 37162 Note that major ticks take precedence over minor ticks.  */);
 37163   display_line_numbers_major_tick = 0;
 37164 
 37165   DEFVAR_INT ("display-line-numbers-minor-tick", display_line_numbers_minor_tick,
 37166     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37167 The line number is shown with the `line-number-minor-tick' face.
 37168 Otherwise, no special highlighting is done every Nth line.
 37169 Note that major ticks take precedence over minor ticks.  */);
 37170   display_line_numbers_minor_tick = 0;
 37171 
 37172   DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
 37173     doc: /* Non-nil means don't eval Lisp during redisplay.  */);
 37174   inhibit_eval_during_redisplay = false;
 37175 
 37176   DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
 37177     doc: /* Non-nil means don't free realized faces.  Internal use only.  */);
 37178   inhibit_free_realized_faces = false;
 37179 
 37180   DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
 37181     doc: /* Non-nil means don't mirror characters even when bidi context requires that.
 37182 Intended for use during debugging and for testing bidi display;
 37183 see biditest.el in the test suite.  */);
 37184   inhibit_bidi_mirroring = false;
 37185 
 37186   DEFVAR_BOOL ("bidi-inhibit-bpa", bidi_inhibit_bpa,
 37187     doc: /* Non-nil means inhibit the Bidirectional Parentheses Algorithm.
 37188 Disabling the BPA makes redisplay faster, but might produce incorrect
 37189 display reordering of bidirectional text with embedded parentheses and
 37190 other bracket characters whose `paired-bracket' Unicode property is
 37191 non-nil, see `get-char-code-property'.  */);
 37192   bidi_inhibit_bpa = false;
 37193 
 37194 #ifdef GLYPH_DEBUG
 37195   DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
 37196                doc: /* Inhibit try_window_id display optimization.  */);
 37197   inhibit_try_window_id = false;
 37198 
 37199   DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
 37200                doc: /* Inhibit try_window_reusing display optimization.  */);
 37201   inhibit_try_window_reusing = false;
 37202 
 37203   DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
 37204                doc: /* Inhibit try_cursor_movement display optimization.  */);
 37205   inhibit_try_cursor_movement = false;
 37206 #endif /* GLYPH_DEBUG */
 37207 
 37208   DEFVAR_INT ("overline-margin", overline_margin,
 37209                doc: /* Space between overline and text, in pixels.
 37210 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
 37211 margin to the character height.  */);
 37212   overline_margin = 2;
 37213 
 37214   DEFVAR_INT ("underline-minimum-offset",
 37215                underline_minimum_offset,
 37216      doc: /* Minimum distance between baseline and underline.
 37217 This can improve legibility of underlined text at small font sizes,
 37218 particularly when using variable `x-use-underline-position-properties'
 37219 with fonts that specify an UNDERLINE_POSITION relatively close to the
 37220 baseline.  The default value is 1.  */);
 37221   underline_minimum_offset = 1;
 37222   DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
 37223 
 37224   DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
 37225                doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
 37226 This feature only works when on a window system that can change
 37227 cursor shapes.  */);
 37228   display_hourglass_p = true;
 37229 
 37230   DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
 37231                doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy.  */);
 37232   Vhourglass_delay = make_fixnum (DEFAULT_HOURGLASS_DELAY);
 37233 
 37234 #ifdef HAVE_WINDOW_SYSTEM
 37235   hourglass_atimer = NULL;
 37236   hourglass_shown_p = false;
 37237 #endif /* HAVE_WINDOW_SYSTEM */
 37238 
 37239   /* Name of the face used to display glyphless characters.  */
 37240   DEFSYM (Qglyphless_char, "glyphless-char");
 37241 
 37242   /* Method symbols for Vglyphless_char_display.  */
 37243   DEFSYM (Qhex_code, "hex-code");
 37244   DEFSYM (Qempty_box, "empty-box");
 37245   DEFSYM (Qthin_space, "thin-space");
 37246   DEFSYM (Qzero_width, "zero-width");
 37247 
 37248   DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
 37249                doc: /* Function run just before redisplay.
 37250 It is called with one argument, which is the set of windows that are to
 37251 be redisplayed.  This set can be nil (meaning, only the selected window),
 37252 or t (meaning all windows).  */);
 37253   Vpre_redisplay_function = intern ("ignore");
 37254 
 37255   /* Symbol for the purpose of Vglyphless_char_display.  */
 37256   DEFSYM (Qglyphless_char_display, "glyphless-char-display");
 37257   Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_fixnum (1));
 37258 
 37259   DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
 37260                doc: /* Char-table defining glyphless characters.
 37261 Each element, if non-nil, should be one of the following:
 37262   an ASCII acronym string: display this string in a box
 37263   `hex-code':   display the hexadecimal code of a character in a box
 37264   `empty-box':  display as an empty box
 37265   `thin-space': display as 1-pixel width space
 37266   `zero-width': don't display
 37267 Any other value is interpreted as `empty-box'.
 37268 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
 37269 display method for graphical terminals and text terminals respectively.
 37270 GRAPHICAL and TEXT should each have one of the values listed above.
 37271 
 37272 The char-table has one extra slot to control the display of characters
 37273 for which no font is found on graphical terminals, and characters that
 37274 cannot be displayed by text-mode terminals.  Its value should be an
 37275 ASCII acronym string, `hex-code', `empty-box', or `thin-space'.  It
 37276 could also be a cons cell of any two of these, to specify separate
 37277 values for graphical and text terminals.  The default is `empty-box'.
 37278 
 37279 With the obvious exception of `zero-width', all the other representations
 37280 are displayed using the face `glyphless-char'.
 37281 
 37282 If a character has a non-nil entry in an active display table, the
 37283 display table takes effect; in this case, Emacs does not consult
 37284 `glyphless-char-display' at all.  */);
 37285   Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
 37286   Fset_char_table_extra_slot (Vglyphless_char_display, make_fixnum (0),
 37287                               Qempty_box);
 37288 
 37289   DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
 37290                doc: /* If non-nil, debug if a message matching this regexp is displayed.  */);
 37291   Vdebug_on_message = Qnil;
 37292 
 37293   DEFVAR_LISP ("set-message-function", Vset_message_function,
 37294                doc: /* If non-nil, function to handle display of echo-area messages.
 37295 The function is called with one argument that is the text of a message.
 37296 If this function returns nil, the message is displayed in the echo area
 37297 as usual.  If the function returns a string, the returned string is
 37298 displayed in the echo area.  If this function returns any other non-nil
 37299 value, this means that the message was already handled, and the original
 37300 message text will not be displayed in the echo area.
 37301 
 37302 Also see `clear-message-function' (which can be used to clear the
 37303 message displayed by this function), and `command-error-function'
 37304 (which controls how error messages are displayed).  */);
 37305   Vset_message_function = Qnil;
 37306 
 37307   DEFSYM (Qdont_clear_message, "dont-clear-message");
 37308   DEFVAR_LISP ("clear-message-function", Vclear_message_function,
 37309                doc: /* If non-nil, function to clear echo-area messages.
 37310 Usually this function is called when the next input event arrives.
 37311 It is expected to clear the message displayed by its counterpart
 37312 function specified by `set-message-function'.
 37313 
 37314 The function is called without arguments.
 37315 
 37316 If this function returns a value that isn't `dont-clear-message', the
 37317 message is cleared from the echo area as usual.  If this function
 37318 returns `dont-clear-message', this means that the message was already
 37319 handled, and the original message text will not be cleared from the
 37320 echo area.  */);
 37321   Vclear_message_function = Qnil;
 37322 
 37323   DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
 37324                doc: /* Code of the cause for redisplaying all windows.
 37325 Internal use only.  */);
 37326   Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
 37327 
 37328   DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
 37329                doc: /* Code of the cause for redisplaying mode lines.
 37330 Internal use only.  */);
 37331   Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
 37332 
 37333   DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
 37334      doc: /* Non-nil means it is not safe to attempt bidi reordering for display.  */);
 37335   /* Initialize to t, since we need to disable reordering until
 37336      loadup.el successfully loads charprop.el.  */
 37337   redisplay__inhibit_bidi = true;
 37338 
 37339   DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
 37340     doc: /* Non-nil means display raw bytes in hexadecimal format.
 37341 The default is to use octal format (\\200) whereas hexadecimal (\\x80)
 37342 may be more familiar to users.  */);
 37343   display_raw_bytes_as_hex = false;
 37344 
 37345   DEFVAR_BOOL ("mouse-fine-grained-tracking", mouse_fine_grained_tracking,
 37346     doc: /* Non-nil for pixel-wise mouse-movement.
 37347 When nil, mouse-movement events will not be generated as long as the
 37348 mouse stays within the extent of a single glyph (except for images).  */);
 37349   mouse_fine_grained_tracking = false;
 37350 
 37351   DEFVAR_BOOL ("tab-bar--dragging-in-progress", tab_bar__dragging_in_progress,
 37352     doc: /* Non-nil when maybe dragging tab bar item.  */);
 37353   tab_bar__dragging_in_progress = false;
 37354 
 37355   DEFVAR_BOOL ("redisplay-skip-initial-frame", redisplay_skip_initial_frame,
 37356     doc: /* Non-nil means skip redisplay of the initial frame.
 37357 The initial frame is the text-mode frame used by Emacs internally during
 37358 the early stages of startup.  That frame is not displayed anywhere, so
 37359 skipping it is best except in special circumstances such as running
 37360 redisplay tests in batch mode.   */);
 37361   redisplay_skip_initial_frame = true;
 37362 
 37363   DEFVAR_BOOL ("redisplay-skip-fontification-on-input",
 37364                redisplay_skip_fontification_on_input,
 37365     doc: /* Skip `fontification_functions` when there is input pending.
 37366 If non-nil and there was input pending at the beginning of the command,
 37367 the `fontification_functions` hook is not run.  This usually does not
 37368 affect the display because redisplay is completely skipped anyway if input
 37369 was pending, but it can make scrolling smoother by avoiding
 37370 unnecessary fontification.
 37371 It is similar to `fast-but-imprecise-scrolling' with similar tradeoffs,
 37372 but with the advantage that it should only affect the behavior when Emacs
 37373 has trouble keeping up with the incoming input rate.  */);
 37374   redisplay_skip_fontification_on_input = false;
 37375 
 37376   DEFVAR_BOOL ("redisplay-adhoc-scroll-in-resize-mini-windows",
 37377                redisplay_adhoc_scroll_in_resize_mini_windows,
 37378     doc: /* If nil always use normal scrolling in minibuffer windows.
 37379 Otherwise, use custom-tailored code after resizing minibuffer windows to try
 37380 and display the most important part of the minibuffer.   */);
 37381   /* See bug#43519 for some discussion around this.  */
 37382   redisplay_adhoc_scroll_in_resize_mini_windows = true;
 37383 
 37384   DEFVAR_BOOL ("composition-break-at-point", composition_break_at_point,
 37385     doc: /* If non-nil, prevent auto-composition of characters around point.
 37386 This makes it easier to edit character sequences that are
 37387 composed on display.  */);
 37388   composition_break_at_point = false;
 37389 
 37390   DEFVAR_INT ("max-redisplay-ticks", max_redisplay_ticks,
 37391     doc: /* Maximum number of redisplay ticks before aborting redisplay of a window.
 37392 
 37393 This allows to abort the display of a window if the amount of low-level
 37394 redisplay operations exceeds the value of this variable.  When display of
 37395 a window is aborted due to this reason, the buffer shown in that window
 37396 will not have its windows redisplayed until the buffer is modified or until
 37397 you type \\[recenter-top-bottom] with one of its windows selected.
 37398 You can also decide to kill the buffer and visit it in some
 37399 other way, like under `so-long-mode' or literally.
 37400 
 37401 The default value is zero, which disables this feature.
 37402 The recommended non-zero value is between 100000 and 1000000,
 37403 depending on your patience and the speed of your system.  */);
 37404   max_redisplay_ticks = 0;
 37405 }
 37406 
 37407 
 37408 /* Initialize this module when Emacs starts.  */
 37409 
 37410 void
 37411 init_xdisp (void)
 37412 {
 37413   CHARPOS (this_line_start_pos) = 0;
 37414 
 37415   echo_area_window = minibuf_window;
 37416 
 37417   if (!noninteractive)
 37418     {
 37419       struct window *m = XWINDOW (minibuf_window);
 37420       Lisp_Object frame = m->frame;
 37421       struct frame *f = XFRAME (frame);
 37422       Lisp_Object root = FRAME_ROOT_WINDOW (f);
 37423       struct window *r = XWINDOW (root);
 37424       int i;
 37425 
 37426       r->top_line = FRAME_TOP_MARGIN (f);
 37427       r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
 37428       r->total_cols = FRAME_COLS (f);
 37429       r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
 37430       r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
 37431       r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
 37432 
 37433       m->top_line = FRAME_TOTAL_LINES (f) - 1;
 37434       m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
 37435       m->total_cols = FRAME_COLS (f);
 37436       m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
 37437       m->total_lines = 1;
 37438       m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
 37439 
 37440       scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
 37441       scratch_glyph_row.glyphs[TEXT_AREA + 1]
 37442         = scratch_glyphs + MAX_SCRATCH_GLYPHS;
 37443 
 37444       /* The default ellipsis glyphs `...'.  */
 37445       for (i = 0; i < 3; ++i)
 37446         default_invis_vector[i] = make_fixnum ('.');
 37447     }
 37448 
 37449   {
 37450     /* Allocate the buffer for frame titles.
 37451        Also used for `format-mode-line'.  */
 37452     int size = 100;
 37453     mode_line_noprop_buf = xmalloc (size);
 37454     mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 37455     mode_line_noprop_ptr = mode_line_noprop_buf;
 37456     mode_line_target = MODE_LINE_DISPLAY;
 37457   }
 37458 
 37459   help_echo_showing_p = false;
 37460 }
 37461 
 37462 #ifdef HAVE_WINDOW_SYSTEM
 37463 
 37464 /* Platform-independent portion of hourglass implementation.  */
 37465 
 37466 /* Timer function of hourglass_atimer.  */
 37467 
 37468 static void
 37469 show_hourglass (struct atimer *timer)
 37470 {
 37471   /* The timer implementation will cancel this timer automatically
 37472      after this function has run.  Set hourglass_atimer to null
 37473      so that we know the timer doesn't have to be canceled.  */
 37474   hourglass_atimer = NULL;
 37475 
 37476   if (!hourglass_shown_p)
 37477     {
 37478       Lisp_Object tail, frame;
 37479 
 37480       block_input ();
 37481 
 37482       FOR_EACH_FRAME (tail, frame)
 37483         {
 37484           struct frame *f = XFRAME (frame);
 37485 
 37486           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37487               && FRAME_RIF (f)->show_hourglass)
 37488             FRAME_RIF (f)->show_hourglass (f);
 37489         }
 37490 
 37491       hourglass_shown_p = true;
 37492       unblock_input ();
 37493     }
 37494 }
 37495 
 37496 /* Cancel a currently active hourglass timer, and start a new one.  */
 37497 
 37498 void
 37499 start_hourglass (void)
 37500 {
 37501   struct timespec delay;
 37502 
 37503   cancel_hourglass ();
 37504 
 37505   if (FIXNUMP (Vhourglass_delay)
 37506       && XFIXNUM (Vhourglass_delay) > 0)
 37507     delay = make_timespec (min (XFIXNUM (Vhourglass_delay),
 37508                                   TYPE_MAXIMUM (time_t)),
 37509                            0);
 37510   else if (FLOATP (Vhourglass_delay)
 37511            && XFLOAT_DATA (Vhourglass_delay) > 0)
 37512     delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
 37513   else
 37514     delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
 37515 
 37516   hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
 37517                                    show_hourglass, NULL);
 37518 }
 37519 
 37520 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
 37521    shown.  */
 37522 
 37523 void
 37524 cancel_hourglass (void)
 37525 {
 37526   if (hourglass_atimer)
 37527     {
 37528       cancel_atimer (hourglass_atimer);
 37529       hourglass_atimer = NULL;
 37530     }
 37531 
 37532   if (hourglass_shown_p)
 37533     {
 37534       Lisp_Object tail, frame;
 37535 
 37536       block_input ();
 37537 
 37538       FOR_EACH_FRAME (tail, frame)
 37539         {
 37540           struct frame *f = XFRAME (frame);
 37541 
 37542           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37543               && FRAME_RIF (f)->hide_hourglass)
 37544             FRAME_RIF (f)->hide_hourglass (f);
 37545 #ifdef HAVE_NTGUI
 37546           /* No cursors on non GUI frames - restore to stock arrow cursor.  */
 37547           else if (!FRAME_W32_P (f))
 37548             w32_arrow_cursor ();
 37549 #endif
 37550         }
 37551 
 37552       hourglass_shown_p = false;
 37553       unblock_input ();
 37554     }
 37555 }
 37556 
 37557 /* Return a correction to be applied to G->pixel_width when it is
 37558    displayed in MOUSE_FACE.  This is needed for the first and the last
 37559    glyphs of text inside a face with :box when it is displayed with
 37560    MOUSE_FACE that has a different or no :box attribute.
 37561    ORIGINAL_FACE is the face G was originally drawn in, and MOUSE_FACE
 37562    is the face it will be drawn in now.  ROW is the G's glyph row and
 37563    W is its window.  */
 37564 static int
 37565 adjust_glyph_width_for_mouse_face (struct glyph *g, struct glyph_row *row,
 37566                                    struct window *w,
 37567                                    struct face *original_face,
 37568                                    struct face *mouse_face)
 37569 {
 37570   int sum = 0;
 37571 
 37572   bool do_left_box_p = g->left_box_line_p;
 37573   bool do_right_box_p = g->right_box_line_p;
 37574 
 37575   /* This is required because we test some parameters of the image
 37576      slice before applying the box in produce_image_glyph.  */
 37577   if (g->type == IMAGE_GLYPH)
 37578     {
 37579       if (!row->reversed_p)
 37580         {
 37581           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37582                                              g->u.img_id);
 37583           do_left_box_p = g->left_box_line_p &&
 37584             g->slice.img.x == 0;
 37585           do_right_box_p = g->right_box_line_p &&
 37586             g->slice.img.x + g->slice.img.width == img->width;
 37587         }
 37588       else
 37589         {
 37590           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37591                                              g->u.img_id);
 37592           do_left_box_p = g->left_box_line_p &&
 37593             g->slice.img.x + g->slice.img.width == img->width;
 37594           do_right_box_p = g->right_box_line_p &&
 37595             g->slice.img.x == 0;
 37596         }
 37597     }
 37598 
 37599   /* If the glyph has a left box line, subtract it from the offset.  */
 37600   if (do_left_box_p)
 37601     sum -= max (0, original_face->box_vertical_line_width);
 37602   /* Likewise with the right box line, as there may be a
 37603      box there as well.  */
 37604   if (do_right_box_p)
 37605     sum -= max (0, original_face->box_vertical_line_width);
 37606   /* Now add the line widths from the new face.  */
 37607   if (g->left_box_line_p)
 37608     sum += max (0, mouse_face->box_vertical_line_width);
 37609   if (g->right_box_line_p)
 37610     sum += max (0, mouse_face->box_vertical_line_width);
 37611 
 37612   return sum;
 37613 }
 37614 
 37615 /* Get the offset due to mouse-highlight to apply before drawing
 37616    phys_cursor, and return it in OFFSET.  ROW should be the row that
 37617    is under mouse face and contains the phys cursor.
 37618 
 37619    This is required because the produce_XXX_glyph series of functions
 37620    add the width of the various vertical box lines to the total width
 37621    of the glyphs, but that must be updated when the row is put under
 37622    mouse face, which can have different box dimensions.  */
 37623 static void
 37624 get_cursor_offset_for_mouse_face (struct window *w, struct glyph_row *row,
 37625                                   int *offset)
 37626 {
 37627   int sum = 0;
 37628   /* Return because the mode line can't possibly have a cursor. */
 37629   if (row->mode_line_p)
 37630     return;
 37631 
 37632   block_input ();
 37633 
 37634   struct frame *f = WINDOW_XFRAME (w);
 37635   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 37636   struct glyph *start, *end;
 37637   struct face *mouse_face = FACE_FROM_ID (f, hlinfo->mouse_face_face_id);
 37638   int hpos = w->phys_cursor.hpos;
 37639   end = &row->glyphs[TEXT_AREA][hpos];
 37640 
 37641   if (!row->reversed_p)
 37642     {
 37643       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 37644           hlinfo->mouse_face_beg_row)
 37645         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_beg_col];
 37646       else
 37647         start = row->glyphs[TEXT_AREA];
 37648     }
 37649   else
 37650     {
 37651       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 37652           hlinfo->mouse_face_end_row)
 37653         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_end_col];
 37654       else
 37655         start = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
 37656     }
 37657 
 37658   /* Calculate the offset by which to correct phys_cursor x if we are
 37659      drawing the cursor inside mouse-face highlighted text.  */
 37660 
 37661   for ( ; row->reversed_p ? start > end : start < end;
 37662           row->reversed_p ? --start : ++start)
 37663     sum += adjust_glyph_width_for_mouse_face (start, row, w,
 37664                                               FACE_FROM_ID (f, start->face_id),
 37665                                               mouse_face);
 37666 
 37667   if (row->reversed_p)
 37668     sum = -sum;
 37669 
 37670   *offset = sum;
 37671 
 37672   unblock_input ();
 37673 }
 37674 #endif /* HAVE_WINDOW_SYSTEM */

/* [<][>][^][v][top][bottom][index][help] */